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 | |
| 45 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 46 | |
| 47 | /******************** cert_key_and_chain functions ************************* |
| 48 | * These are the functions that fills a cert_key_and_chain structure. For the |
| 49 | * functions filling a SSL_CTX from a cert_key_and_chain, see ssl_sock.c |
| 50 | */ |
| 51 | |
| 52 | /* |
| 53 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 54 | * makes only basic test if the data seems like SCTL. No signature validation |
| 55 | * is performed. |
| 56 | */ |
| 57 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
| 58 | { |
| 59 | int ret = 1; |
| 60 | int len, pos, sct_len; |
| 61 | unsigned char *data; |
| 62 | |
| 63 | if (sctl->data < 2) |
| 64 | goto out; |
| 65 | |
| 66 | data = (unsigned char *) sctl->area; |
| 67 | len = (data[0] << 8) | data[1]; |
| 68 | |
| 69 | if (len + 2 != sctl->data) |
| 70 | goto out; |
| 71 | |
| 72 | data = data + 2; |
| 73 | pos = 0; |
| 74 | while (pos < len) { |
| 75 | if (len - pos < 2) |
| 76 | goto out; |
| 77 | |
| 78 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 79 | if (pos + sct_len + 2 > len) |
| 80 | goto out; |
| 81 | |
| 82 | pos += sct_len + 2; |
| 83 | } |
| 84 | |
| 85 | ret = 0; |
| 86 | |
| 87 | out: |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | /* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path> |
| 92 | * It fills the ckch->sctl buffer |
| 93 | * return 0 on success or != 0 on failure */ |
| 94 | int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 95 | { |
| 96 | int fd = -1; |
| 97 | int r = 0; |
| 98 | int ret = 1; |
| 99 | struct buffer tmp; |
| 100 | struct buffer *src; |
| 101 | struct buffer *sctl; |
| 102 | |
| 103 | if (buf) { |
William Lallemand | 7cdbb56 | 2021-01-27 14:58:51 +0100 | [diff] [blame] | 104 | chunk_initstr(&tmp, buf); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 105 | src = &tmp; |
| 106 | } else { |
| 107 | fd = open(sctl_path, O_RDONLY); |
| 108 | if (fd == -1) |
| 109 | goto end; |
| 110 | |
| 111 | trash.data = 0; |
| 112 | while (trash.data < trash.size) { |
| 113 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 114 | if (r < 0) { |
| 115 | if (errno == EINTR) |
| 116 | continue; |
| 117 | goto end; |
| 118 | } |
| 119 | else if (r == 0) { |
| 120 | break; |
| 121 | } |
| 122 | trash.data += r; |
| 123 | } |
| 124 | src = &trash; |
| 125 | } |
| 126 | |
| 127 | ret = ssl_sock_parse_sctl(src); |
| 128 | if (ret) |
| 129 | goto end; |
| 130 | |
| 131 | sctl = calloc(1, sizeof(*sctl)); |
| 132 | if (!chunk_dup(sctl, src)) { |
| 133 | free(sctl); |
| 134 | sctl = NULL; |
| 135 | goto end; |
| 136 | } |
| 137 | /* no error, fill ckch with new context, old context must be free */ |
| 138 | if (ckch->sctl) { |
| 139 | free(ckch->sctl->area); |
| 140 | ckch->sctl->area = NULL; |
| 141 | free(ckch->sctl); |
| 142 | } |
| 143 | ckch->sctl = sctl; |
| 144 | ret = 0; |
| 145 | end: |
| 146 | if (fd != -1) |
| 147 | close(fd); |
| 148 | |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
| 153 | /* |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 154 | * 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] | 155 | * path 'ocsp_path' or base64 in a buffer <buf> |
| 156 | * |
| 157 | * Returns 0 on success, 1 in error case. |
| 158 | */ |
| 159 | int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 160 | { |
| 161 | int fd = -1; |
| 162 | int r = 0; |
| 163 | int ret = 1; |
| 164 | struct buffer *ocsp_response; |
| 165 | struct buffer *src = NULL; |
| 166 | |
| 167 | if (buf) { |
| 168 | int i, j; |
| 169 | /* if it's from a buffer it will be base64 */ |
| 170 | |
| 171 | /* remove \r and \n from the payload */ |
| 172 | for (i = 0, j = 0; buf[i]; i++) { |
| 173 | if (buf[i] == '\r' || buf[i] == '\n') |
| 174 | continue; |
| 175 | buf[j++] = buf[i]; |
| 176 | } |
| 177 | buf[j] = 0; |
| 178 | |
| 179 | ret = base64dec(buf, j, trash.area, trash.size); |
| 180 | if (ret < 0) { |
| 181 | memprintf(err, "Error reading OCSP response in base64 format"); |
| 182 | goto end; |
| 183 | } |
| 184 | trash.data = ret; |
| 185 | src = &trash; |
| 186 | } else { |
| 187 | fd = open(ocsp_path, O_RDONLY); |
| 188 | if (fd == -1) { |
| 189 | memprintf(err, "Error opening OCSP response file"); |
| 190 | goto end; |
| 191 | } |
| 192 | |
| 193 | trash.data = 0; |
| 194 | while (trash.data < trash.size) { |
| 195 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 196 | if (r < 0) { |
| 197 | if (errno == EINTR) |
| 198 | continue; |
| 199 | |
| 200 | memprintf(err, "Error reading OCSP response from file"); |
| 201 | goto end; |
| 202 | } |
| 203 | else if (r == 0) { |
| 204 | break; |
| 205 | } |
| 206 | trash.data += r; |
| 207 | } |
| 208 | close(fd); |
| 209 | fd = -1; |
| 210 | src = &trash; |
| 211 | } |
| 212 | |
| 213 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 214 | if (!chunk_dup(ocsp_response, src)) { |
| 215 | free(ocsp_response); |
| 216 | ocsp_response = NULL; |
| 217 | goto end; |
| 218 | } |
| 219 | /* no error, fill ckch with new context, old context must be free */ |
| 220 | if (ckch->ocsp_response) { |
| 221 | free(ckch->ocsp_response->area); |
| 222 | ckch->ocsp_response->area = NULL; |
| 223 | free(ckch->ocsp_response); |
| 224 | } |
| 225 | ckch->ocsp_response = ocsp_response; |
| 226 | ret = 0; |
| 227 | end: |
| 228 | if (fd != -1) |
| 229 | close(fd); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | /* |
| 236 | * Try to load in a ckch every files related to a ckch. |
| 237 | * (PEM, sctl, ocsp, issuer etc.) |
| 238 | * |
| 239 | * This function is only used to load files during the configuration parsing, |
| 240 | * it is not used with the CLI. |
| 241 | * |
| 242 | * This allows us to carry the contents of the file without having to read the |
| 243 | * file multiple times. The caller must call |
| 244 | * ssl_sock_free_cert_key_and_chain_contents. |
| 245 | * |
| 246 | * returns: |
| 247 | * 0 on Success |
| 248 | * 1 on SSL Failure |
| 249 | */ |
| 250 | int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 251 | { |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 252 | struct buffer *fp = NULL; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 253 | int ret = 1; |
| 254 | |
| 255 | /* try to load the PEM */ |
| 256 | if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) { |
| 257 | goto end; |
| 258 | } |
| 259 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 260 | fp = alloc_trash_chunk(); |
| 261 | if (!fp) { |
| 262 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 263 | goto end; |
| 264 | } |
| 265 | |
| 266 | if (!chunk_strcpy(fp, path) || (b_data(fp) > MAXPATHLEN)) { |
| 267 | memprintf(err, "%s '%s' filename too long'.\n", |
| 268 | err && *err ? *err : "", fp->area); |
| 269 | ret = 1; |
| 270 | goto end; |
| 271 | } |
| 272 | |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 273 | /* remove the ".crt" extension */ |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 274 | if (global_ssl.extra_files_noext) { |
| 275 | char *ext; |
| 276 | |
| 277 | /* look for the extension */ |
| 278 | if ((ext = strrchr(fp->area, '.'))) { |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 279 | |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 280 | if (!strcmp(ext, ".crt")) { |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 281 | *ext = '\0'; |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 282 | fp->data = strlen(fp->area); |
| 283 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | } |
| 287 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 288 | /* try to load an external private key if it wasn't in the PEM */ |
| 289 | if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 290 | struct stat st; |
| 291 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 292 | |
| 293 | if (!chunk_strcat(fp, ".key") || (b_data(fp) > MAXPATHLEN)) { |
| 294 | memprintf(err, "%s '%s' filename too long'.\n", |
| 295 | err && *err ? *err : "", fp->area); |
| 296 | ret = 1; |
| 297 | goto end; |
| 298 | } |
| 299 | |
| 300 | if (stat(fp->area, &st) == 0) { |
| 301 | if (ssl_sock_load_key_into_ckch(fp->area, NULL, ckch, err)) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 302 | 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] | 303 | err && *err ? *err : "", fp->area); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 304 | goto end; |
| 305 | } |
| 306 | } |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 307 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 308 | if (ckch->key == NULL) { |
| 309 | memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area); |
| 310 | goto end; |
| 311 | } |
| 312 | /* remove the added extension */ |
| 313 | *(fp->area + fp->data - strlen(".key")) = '\0'; |
| 314 | b_sub(fp, strlen(".key")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | if (!X509_check_private_key(ckch->cert, ckch->key)) { |
| 318 | memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n", |
| 319 | err && *err ? *err : "", path); |
| 320 | goto end; |
| 321 | } |
| 322 | |
Ilya Shipitsin | 8702b26 | 2021-02-13 11:45:33 +0500 | [diff] [blame] | 323 | #ifdef HAVE_SSL_SCTL |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 324 | /* try to load the sctl file */ |
| 325 | if (global_ssl.extra_files & SSL_GF_SCTL) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 326 | struct stat st; |
| 327 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 328 | if (!chunk_strcat(fp, ".sctl") || b_data(fp) > MAXPATHLEN) { |
| 329 | memprintf(err, "%s '%s' filename too long'.\n", |
| 330 | err && *err ? *err : "", fp->area); |
| 331 | ret = 1; |
| 332 | goto end; |
| 333 | } |
| 334 | |
| 335 | if (stat(fp->area, &st) == 0) { |
| 336 | if (ssl_sock_load_sctl_from_file(fp->area, NULL, ckch, err)) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 337 | 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] | 338 | err && *err ? *err : "", fp->area); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 339 | ret = 1; |
| 340 | goto end; |
| 341 | } |
| 342 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 343 | /* remove the added extension */ |
| 344 | *(fp->area + fp->data - strlen(".sctl")) = '\0'; |
| 345 | b_sub(fp, strlen(".sctl")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 346 | } |
| 347 | #endif |
| 348 | |
| 349 | /* try to load an ocsp response file */ |
| 350 | if (global_ssl.extra_files & SSL_GF_OCSP) { |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 351 | struct stat st; |
| 352 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 353 | if (!chunk_strcat(fp, ".ocsp") || b_data(fp) > MAXPATHLEN) { |
| 354 | memprintf(err, "%s '%s' filename too long'.\n", |
| 355 | err && *err ? *err : "", fp->area); |
| 356 | ret = 1; |
| 357 | goto end; |
| 358 | } |
| 359 | |
| 360 | if (stat(fp->area, &st) == 0) { |
| 361 | 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] | 362 | ret = 1; |
| 363 | goto end; |
| 364 | } |
| 365 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 366 | /* remove the added extension */ |
| 367 | *(fp->area + fp->data - strlen(".ocsp")) = '\0'; |
| 368 | b_sub(fp, strlen(".ocsp")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | #ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */ |
| 372 | if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) { |
| 373 | /* if no issuer was found, try to load an issuer from the .issuer */ |
| 374 | if (!ckch->ocsp_issuer) { |
| 375 | struct stat st; |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 376 | |
| 377 | if (!chunk_strcat(fp, ".issuer") || b_data(fp) > MAXPATHLEN) { |
| 378 | memprintf(err, "%s '%s' filename too long'.\n", |
| 379 | err && *err ? *err : "", fp->area); |
| 380 | ret = 1; |
| 381 | goto end; |
| 382 | } |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 383 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 384 | if (stat(fp->area, &st) == 0) { |
| 385 | 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] | 386 | ret = 1; |
| 387 | goto end; |
| 388 | } |
| 389 | |
| 390 | if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) { |
| 391 | memprintf(err, "%s '%s' is not an issuer'.\n", |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 392 | err && *err ? *err : "", fp->area); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 393 | ret = 1; |
| 394 | goto end; |
| 395 | } |
| 396 | } |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 397 | /* remove the added extension */ |
| 398 | *(fp->area + fp->data - strlen(".issuer")) = '\0'; |
| 399 | b_sub(fp, strlen(".issuer")); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | #endif |
| 403 | |
| 404 | ret = 0; |
| 405 | |
| 406 | end: |
| 407 | |
| 408 | ERR_clear_error(); |
| 409 | |
| 410 | /* Something went wrong in one of the reads */ |
| 411 | if (ret != 0) |
| 412 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 413 | |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 414 | free_trash_chunk(fp); |
| 415 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Try to load a private key file from a <path> or a buffer <buf> |
| 421 | * |
| 422 | * If it failed you should not attempt to use the ckch but free it. |
| 423 | * |
| 424 | * Return 0 on success or != 0 on failure |
| 425 | */ |
| 426 | int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 427 | { |
| 428 | BIO *in = NULL; |
| 429 | int ret = 1; |
| 430 | EVP_PKEY *key = NULL; |
| 431 | |
| 432 | if (buf) { |
| 433 | /* reading from a buffer */ |
| 434 | in = BIO_new_mem_buf(buf, -1); |
| 435 | if (in == NULL) { |
| 436 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 437 | goto end; |
| 438 | } |
| 439 | |
| 440 | } else { |
| 441 | /* reading from a file */ |
| 442 | in = BIO_new(BIO_s_file()); |
| 443 | if (in == NULL) |
| 444 | goto end; |
| 445 | |
| 446 | if (BIO_read_filename(in, path) <= 0) |
| 447 | goto end; |
| 448 | } |
| 449 | |
| 450 | /* Read Private Key */ |
| 451 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 452 | if (key == NULL) { |
| 453 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 454 | err && *err ? *err : "", path); |
| 455 | goto end; |
| 456 | } |
| 457 | |
| 458 | ret = 0; |
| 459 | |
| 460 | SWAP(ckch->key, key); |
| 461 | |
| 462 | end: |
| 463 | |
| 464 | ERR_clear_error(); |
| 465 | if (in) |
| 466 | BIO_free(in); |
| 467 | if (key) |
| 468 | EVP_PKEY_free(key); |
| 469 | |
| 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Try to load a PEM file from a <path> or a buffer <buf> |
| 475 | * The PEM must contain at least a Certificate, |
| 476 | * It could contain a DH, a certificate chain and a PrivateKey. |
| 477 | * |
| 478 | * If it failed you should not attempt to use the ckch but free it. |
| 479 | * |
| 480 | * Return 0 on success or != 0 on failure |
| 481 | */ |
| 482 | int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
| 483 | { |
| 484 | BIO *in = NULL; |
| 485 | int ret = 1; |
| 486 | X509 *ca; |
| 487 | X509 *cert = NULL; |
| 488 | EVP_PKEY *key = NULL; |
| 489 | DH *dh = NULL; |
| 490 | STACK_OF(X509) *chain = NULL; |
| 491 | |
| 492 | if (buf) { |
| 493 | /* reading from a buffer */ |
| 494 | in = BIO_new_mem_buf(buf, -1); |
| 495 | if (in == NULL) { |
| 496 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 497 | goto end; |
| 498 | } |
| 499 | |
| 500 | } else { |
| 501 | /* reading from a file */ |
| 502 | in = BIO_new(BIO_s_file()); |
| 503 | if (in == NULL) { |
| 504 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 505 | goto end; |
| 506 | } |
| 507 | |
| 508 | if (BIO_read_filename(in, path) <= 0) { |
| 509 | memprintf(err, "%scannot open the file '%s'.\n", |
| 510 | err && *err ? *err : "", path); |
| 511 | goto end; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /* Read Private Key */ |
| 516 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 517 | /* no need to check for errors here, because the private key could be loaded later */ |
| 518 | |
| 519 | #ifndef OPENSSL_NO_DH |
| 520 | /* Seek back to beginning of file */ |
| 521 | if (BIO_reset(in) == -1) { |
| 522 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 523 | err && *err ? *err : "", path); |
| 524 | goto end; |
| 525 | } |
| 526 | |
| 527 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 528 | /* no need to return an error there, dh is not mandatory */ |
| 529 | #endif |
| 530 | |
| 531 | /* Seek back to beginning of file */ |
| 532 | if (BIO_reset(in) == -1) { |
| 533 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 534 | err && *err ? *err : "", path); |
| 535 | goto end; |
| 536 | } |
| 537 | |
| 538 | /* Read Certificate */ |
| 539 | cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 540 | if (cert == NULL) { |
| 541 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 542 | err && *err ? *err : "", path); |
| 543 | goto end; |
| 544 | } |
| 545 | |
| 546 | /* Look for a Certificate Chain */ |
| 547 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 548 | if (chain == NULL) |
| 549 | chain = sk_X509_new_null(); |
| 550 | if (!sk_X509_push(chain, ca)) { |
| 551 | X509_free(ca); |
| 552 | goto end; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | ret = ERR_get_error(); |
| 557 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 558 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 559 | err && *err ? *err : "", path); |
| 560 | goto end; |
| 561 | } |
| 562 | |
| 563 | /* once it loaded the PEM, it should remove everything else in the ckch */ |
| 564 | if (ckch->ocsp_response) { |
| 565 | free(ckch->ocsp_response->area); |
| 566 | ckch->ocsp_response->area = NULL; |
| 567 | free(ckch->ocsp_response); |
| 568 | ckch->ocsp_response = NULL; |
| 569 | } |
| 570 | |
| 571 | if (ckch->sctl) { |
| 572 | free(ckch->sctl->area); |
| 573 | ckch->sctl->area = NULL; |
| 574 | free(ckch->sctl); |
| 575 | ckch->sctl = NULL; |
| 576 | } |
| 577 | |
| 578 | if (ckch->ocsp_issuer) { |
| 579 | X509_free(ckch->ocsp_issuer); |
| 580 | ckch->ocsp_issuer = NULL; |
| 581 | } |
| 582 | |
| 583 | /* no error, fill ckch with new context, old context will be free at end: */ |
| 584 | SWAP(ckch->key, key); |
| 585 | SWAP(ckch->dh, dh); |
| 586 | SWAP(ckch->cert, cert); |
| 587 | SWAP(ckch->chain, chain); |
| 588 | |
| 589 | ret = 0; |
| 590 | |
| 591 | end: |
| 592 | |
| 593 | ERR_clear_error(); |
| 594 | if (in) |
| 595 | BIO_free(in); |
| 596 | if (key) |
| 597 | EVP_PKEY_free(key); |
| 598 | if (dh) |
| 599 | DH_free(dh); |
| 600 | if (cert) |
| 601 | X509_free(cert); |
| 602 | if (chain) |
| 603 | sk_X509_pop_free(chain, X509_free); |
| 604 | |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | /* Frees the contents of a cert_key_and_chain |
| 609 | */ |
| 610 | void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 611 | { |
| 612 | if (!ckch) |
| 613 | return; |
| 614 | |
| 615 | /* Free the certificate and set pointer to NULL */ |
| 616 | if (ckch->cert) |
| 617 | X509_free(ckch->cert); |
| 618 | ckch->cert = NULL; |
| 619 | |
| 620 | /* Free the key and set pointer to NULL */ |
| 621 | if (ckch->key) |
| 622 | EVP_PKEY_free(ckch->key); |
| 623 | ckch->key = NULL; |
| 624 | |
| 625 | /* Free each certificate in the chain */ |
| 626 | if (ckch->chain) |
| 627 | sk_X509_pop_free(ckch->chain, X509_free); |
| 628 | ckch->chain = NULL; |
| 629 | |
| 630 | if (ckch->dh) |
| 631 | DH_free(ckch->dh); |
| 632 | ckch->dh = NULL; |
| 633 | |
| 634 | if (ckch->sctl) { |
| 635 | free(ckch->sctl->area); |
| 636 | ckch->sctl->area = NULL; |
| 637 | free(ckch->sctl); |
| 638 | ckch->sctl = NULL; |
| 639 | } |
| 640 | |
| 641 | if (ckch->ocsp_response) { |
| 642 | free(ckch->ocsp_response->area); |
| 643 | ckch->ocsp_response->area = NULL; |
| 644 | free(ckch->ocsp_response); |
| 645 | ckch->ocsp_response = NULL; |
| 646 | } |
| 647 | |
| 648 | if (ckch->ocsp_issuer) |
| 649 | X509_free(ckch->ocsp_issuer); |
| 650 | ckch->ocsp_issuer = NULL; |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * |
| 655 | * This function copy a cert_key_and_chain in memory |
| 656 | * |
| 657 | * It's used to try to apply changes on a ckch before committing them, because |
| 658 | * most of the time it's not possible to revert those changes |
| 659 | * |
| 660 | * Return a the dst or NULL |
| 661 | */ |
| 662 | struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src, |
| 663 | struct cert_key_and_chain *dst) |
| 664 | { |
William Lallemand | 8f71298 | 2021-02-23 14:45:45 +0100 | [diff] [blame] | 665 | if (!src || !dst) |
| 666 | return NULL; |
| 667 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 668 | if (src->cert) { |
| 669 | dst->cert = src->cert; |
| 670 | X509_up_ref(src->cert); |
| 671 | } |
| 672 | |
| 673 | if (src->key) { |
| 674 | dst->key = src->key; |
| 675 | EVP_PKEY_up_ref(src->key); |
| 676 | } |
| 677 | |
| 678 | if (src->chain) { |
| 679 | dst->chain = X509_chain_up_ref(src->chain); |
| 680 | } |
| 681 | |
| 682 | if (src->dh) { |
| 683 | DH_up_ref(src->dh); |
| 684 | dst->dh = src->dh; |
| 685 | } |
| 686 | |
| 687 | if (src->sctl) { |
| 688 | struct buffer *sctl; |
| 689 | |
| 690 | sctl = calloc(1, sizeof(*sctl)); |
| 691 | if (!chunk_dup(sctl, src->sctl)) { |
| 692 | free(sctl); |
| 693 | sctl = NULL; |
| 694 | goto error; |
| 695 | } |
| 696 | dst->sctl = sctl; |
| 697 | } |
| 698 | |
| 699 | if (src->ocsp_response) { |
| 700 | struct buffer *ocsp_response; |
| 701 | |
| 702 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 703 | if (!chunk_dup(ocsp_response, src->ocsp_response)) { |
| 704 | free(ocsp_response); |
| 705 | ocsp_response = NULL; |
| 706 | goto error; |
| 707 | } |
| 708 | dst->ocsp_response = ocsp_response; |
| 709 | } |
| 710 | |
| 711 | if (src->ocsp_issuer) { |
| 712 | X509_up_ref(src->ocsp_issuer); |
| 713 | dst->ocsp_issuer = src->ocsp_issuer; |
| 714 | } |
| 715 | |
| 716 | return dst; |
| 717 | |
| 718 | error: |
| 719 | |
| 720 | /* free everything */ |
| 721 | ssl_sock_free_cert_key_and_chain_contents(dst); |
| 722 | |
| 723 | return NULL; |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * return 0 on success or != 0 on failure |
| 728 | */ |
| 729 | int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 730 | { |
| 731 | int ret = 1; |
| 732 | BIO *in = NULL; |
| 733 | X509 *issuer; |
| 734 | |
| 735 | if (buf) { |
| 736 | /* reading from a buffer */ |
| 737 | in = BIO_new_mem_buf(buf, -1); |
| 738 | if (in == NULL) { |
| 739 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 740 | goto end; |
| 741 | } |
| 742 | |
| 743 | } else { |
| 744 | /* reading from a file */ |
| 745 | in = BIO_new(BIO_s_file()); |
| 746 | if (in == NULL) |
| 747 | goto end; |
| 748 | |
| 749 | if (BIO_read_filename(in, path) <= 0) |
| 750 | goto end; |
| 751 | } |
| 752 | |
| 753 | issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 754 | if (!issuer) { |
| 755 | memprintf(err, "%s'%s' cannot be read or parsed'.\n", |
| 756 | err && *err ? *err : "", path); |
| 757 | goto end; |
| 758 | } |
| 759 | /* no error, fill ckch with new context, old context must be free */ |
| 760 | if (ckch->ocsp_issuer) |
| 761 | X509_free(ckch->ocsp_issuer); |
| 762 | ckch->ocsp_issuer = issuer; |
| 763 | ret = 0; |
| 764 | |
| 765 | end: |
| 766 | |
| 767 | ERR_clear_error(); |
| 768 | if (in) |
| 769 | BIO_free(in); |
| 770 | |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | /******************** ckch_store functions *********************************** |
| 775 | * The ckch_store is a structure used to cache and index the SSL files used in |
| 776 | * configuration |
| 777 | */ |
| 778 | |
| 779 | /* |
| 780 | * Free a ckch_store, its ckch, its instances and remove it from the ebtree |
| 781 | */ |
| 782 | void ckch_store_free(struct ckch_store *store) |
| 783 | { |
| 784 | struct ckch_inst *inst, *inst_s; |
| 785 | |
| 786 | if (!store) |
| 787 | return; |
| 788 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 789 | ssl_sock_free_cert_key_and_chain_contents(store->ckch); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 790 | |
| 791 | free(store->ckch); |
| 792 | store->ckch = NULL; |
| 793 | |
| 794 | list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) { |
| 795 | ckch_inst_free(inst); |
| 796 | } |
| 797 | ebmb_delete(&store->node); |
| 798 | free(store); |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * create and initialize a ckch_store |
| 803 | * <path> is the key name |
| 804 | * <nmemb> is the number of store->ckch objects to allocate |
| 805 | * |
| 806 | * Return a ckch_store or NULL upon failure. |
| 807 | */ |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 808 | struct ckch_store *ckch_store_new(const char *filename) |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 809 | { |
| 810 | struct ckch_store *store; |
| 811 | int pathlen; |
| 812 | |
| 813 | pathlen = strlen(filename); |
| 814 | store = calloc(1, sizeof(*store) + pathlen + 1); |
| 815 | if (!store) |
| 816 | return NULL; |
| 817 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 818 | memcpy(store->path, filename, pathlen + 1); |
| 819 | |
| 820 | LIST_INIT(&store->ckch_inst); |
| 821 | LIST_INIT(&store->crtlist_entry); |
| 822 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 823 | store->ckch = calloc(1, sizeof(*store->ckch)); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 824 | if (!store->ckch) |
| 825 | goto error; |
| 826 | |
| 827 | return store; |
| 828 | error: |
| 829 | ckch_store_free(store); |
| 830 | return NULL; |
| 831 | } |
| 832 | |
| 833 | /* allocate and duplicate a ckch_store |
| 834 | * Return a new ckch_store or NULL */ |
| 835 | struct ckch_store *ckchs_dup(const struct ckch_store *src) |
| 836 | { |
| 837 | struct ckch_store *dst; |
| 838 | |
William Lallemand | 8f71298 | 2021-02-23 14:45:45 +0100 | [diff] [blame] | 839 | if (!src) |
| 840 | return NULL; |
| 841 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 842 | dst = ckch_store_new(src->path); |
Eric Salama | b77cb98 | 2021-02-23 16:50:57 +0100 | [diff] [blame] | 843 | if (!dst) |
| 844 | return NULL; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 845 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 846 | if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch)) |
| 847 | goto error; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 848 | |
| 849 | return dst; |
| 850 | |
| 851 | error: |
| 852 | ckch_store_free(dst); |
| 853 | |
| 854 | return NULL; |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * lookup a path into the ckchs tree. |
| 859 | */ |
| 860 | struct ckch_store *ckchs_lookup(char *path) |
| 861 | { |
| 862 | struct ebmb_node *eb; |
| 863 | |
| 864 | eb = ebst_lookup(&ckchs_tree, path); |
| 865 | if (!eb) |
| 866 | return NULL; |
| 867 | |
| 868 | return ebmb_entry(eb, struct ckch_store, node); |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * This function allocate a ckch_store and populate it with certificates from files. |
| 873 | */ |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 874 | struct ckch_store *ckchs_load_cert_file(char *path, char **err) |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 875 | { |
| 876 | struct ckch_store *ckchs; |
| 877 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 878 | ckchs = ckch_store_new(path); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 879 | if (!ckchs) { |
| 880 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 881 | goto end; |
| 882 | } |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 883 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 884 | if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1) |
| 885 | goto end; |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 886 | |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 887 | /* insert into the ckchs tree */ |
| 888 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 889 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 890 | return ckchs; |
| 891 | |
| 892 | end: |
| 893 | ckch_store_free(ckchs); |
| 894 | |
| 895 | return NULL; |
| 896 | } |
| 897 | |
William Lallemand | fa1d8b4 | 2020-05-13 15:46:10 +0200 | [diff] [blame] | 898 | |
| 899 | /******************** ckch_inst functions ******************************/ |
| 900 | |
| 901 | /* unlink a ckch_inst, free all SNIs, free the ckch_inst */ |
| 902 | /* The caller must use the lock of the bind_conf if used with inserted SNIs */ |
| 903 | void ckch_inst_free(struct ckch_inst *inst) |
| 904 | { |
| 905 | struct sni_ctx *sni, *sni_s; |
| 906 | |
| 907 | if (inst == NULL) |
| 908 | return; |
| 909 | |
| 910 | list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) { |
| 911 | SSL_CTX_free(sni->ctx); |
| 912 | LIST_DEL(&sni->by_ckch_inst); |
| 913 | ebmb_delete(&sni->name); |
| 914 | free(sni); |
| 915 | } |
Remi Tricot-Le Breton | 96d6028 | 2021-03-31 11:49:27 +0200 | [diff] [blame] | 916 | SSL_CTX_free(inst->ctx); |
| 917 | inst->ctx = NULL; |
William Lallemand | fa1d8b4 | 2020-05-13 15:46:10 +0200 | [diff] [blame] | 918 | LIST_DEL(&inst->by_ckchs); |
| 919 | LIST_DEL(&inst->by_crtlist_entry); |
| 920 | free(inst); |
| 921 | } |
| 922 | |
| 923 | /* Alloc and init a ckch_inst */ |
| 924 | struct ckch_inst *ckch_inst_new() |
| 925 | { |
| 926 | struct ckch_inst *ckch_inst; |
| 927 | |
| 928 | ckch_inst = calloc(1, sizeof *ckch_inst); |
| 929 | if (!ckch_inst) |
| 930 | return NULL; |
| 931 | |
| 932 | LIST_INIT(&ckch_inst->sni_ctx); |
| 933 | LIST_INIT(&ckch_inst->by_ckchs); |
| 934 | LIST_INIT(&ckch_inst->by_crtlist_entry); |
| 935 | |
| 936 | return ckch_inst; |
| 937 | } |
| 938 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 939 | /*************************** CLI commands ***********************/ |
| 940 | |
| 941 | /* Type of SSL payloads that can be updated over the CLI */ |
| 942 | |
| 943 | enum { |
| 944 | CERT_TYPE_PEM = 0, |
| 945 | CERT_TYPE_KEY, |
| 946 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
| 947 | CERT_TYPE_OCSP, |
| 948 | #endif |
| 949 | CERT_TYPE_ISSUER, |
Ilya Shipitsin | 8702b26 | 2021-02-13 11:45:33 +0500 | [diff] [blame] | 950 | #ifdef HAVE_SSL_SCTL |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 951 | CERT_TYPE_SCTL, |
| 952 | #endif |
| 953 | CERT_TYPE_MAX, |
| 954 | }; |
| 955 | |
| 956 | struct { |
| 957 | const char *ext; |
| 958 | int type; |
| 959 | int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err); |
| 960 | /* add a parsing callback */ |
| 961 | } cert_exts[CERT_TYPE_MAX+1] = { |
| 962 | [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */ |
| 963 | [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch }, |
| 964 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
| 965 | [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file }, |
| 966 | #endif |
Ilya Shipitsin | 8702b26 | 2021-02-13 11:45:33 +0500 | [diff] [blame] | 967 | #ifdef HAVE_SSL_SCTL |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 968 | [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file }, |
| 969 | #endif |
| 970 | [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch }, |
| 971 | [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL }, |
| 972 | }; |
| 973 | |
| 974 | |
| 975 | /* release function of the `show ssl cert' command */ |
| 976 | static void cli_release_show_cert(struct appctx *appctx) |
| 977 | { |
| 978 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 979 | } |
| 980 | |
| 981 | /* IO handler of "show ssl cert <filename>" */ |
| 982 | static int cli_io_handler_show_cert(struct appctx *appctx) |
| 983 | { |
| 984 | struct buffer *trash = alloc_trash_chunk(); |
| 985 | struct ebmb_node *node; |
| 986 | struct stream_interface *si = appctx->owner; |
| 987 | struct ckch_store *ckchs; |
| 988 | |
| 989 | if (trash == NULL) |
| 990 | return 1; |
| 991 | |
| 992 | if (!appctx->ctx.ssl.old_ckchs) { |
| 993 | if (ckchs_transaction.old_ckchs) { |
| 994 | ckchs = ckchs_transaction.old_ckchs; |
| 995 | chunk_appendf(trash, "# transaction\n"); |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 996 | chunk_appendf(trash, "*%s\n", ckchs->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
| 1000 | if (!appctx->ctx.cli.p0) { |
| 1001 | chunk_appendf(trash, "# filename\n"); |
| 1002 | node = ebmb_first(&ckchs_tree); |
| 1003 | } else { |
| 1004 | node = &((struct ckch_store *)appctx->ctx.cli.p0)->node; |
| 1005 | } |
| 1006 | while (node) { |
| 1007 | ckchs = ebmb_entry(node, struct ckch_store, node); |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1008 | chunk_appendf(trash, "%s\n", ckchs->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1009 | |
| 1010 | node = ebmb_next(node); |
| 1011 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 1012 | si_rx_room_blk(si); |
| 1013 | goto yield; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | appctx->ctx.cli.p0 = NULL; |
| 1018 | free_trash_chunk(trash); |
| 1019 | return 1; |
| 1020 | yield: |
| 1021 | |
| 1022 | free_trash_chunk(trash); |
| 1023 | appctx->ctx.cli.p0 = ckchs; |
| 1024 | return 0; /* should come back */ |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * Extract and format the DNS SAN extensions and copy result into a chuink |
| 1029 | * Return 0; |
| 1030 | */ |
| 1031 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 1032 | static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out) |
| 1033 | { |
| 1034 | int i; |
| 1035 | char *str; |
| 1036 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 1037 | |
| 1038 | names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 1039 | if (names) { |
| 1040 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 1041 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 1042 | if (i > 0) |
| 1043 | chunk_appendf(out, ", "); |
| 1044 | if (name->type == GEN_DNS) { |
| 1045 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 1046 | chunk_appendf(out, "DNS:%s", str); |
| 1047 | OPENSSL_free(str); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 1052 | } |
| 1053 | return 0; |
| 1054 | } |
| 1055 | #endif |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | /* IO handler of the details "show ssl cert <filename>" */ |
| 1061 | static int cli_io_handler_show_cert_detail(struct appctx *appctx) |
| 1062 | { |
| 1063 | struct stream_interface *si = appctx->owner; |
| 1064 | struct ckch_store *ckchs = appctx->ctx.cli.p0; |
| 1065 | struct buffer *out = alloc_trash_chunk(); |
| 1066 | struct buffer *tmp = alloc_trash_chunk(); |
| 1067 | X509_NAME *name = NULL; |
| 1068 | STACK_OF(X509) *chain; |
| 1069 | unsigned int len = 0; |
| 1070 | int write = -1; |
| 1071 | BIO *bio = NULL; |
| 1072 | int i; |
| 1073 | |
| 1074 | if (!tmp || !out) |
| 1075 | goto end_no_putchk; |
| 1076 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1077 | chunk_appendf(out, "Filename: "); |
| 1078 | if (ckchs == ckchs_transaction.new_ckchs) |
| 1079 | chunk_appendf(out, "*"); |
| 1080 | chunk_appendf(out, "%s\n", ckchs->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1081 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1082 | chunk_appendf(out, "Status: "); |
| 1083 | if (ckchs->ckch->cert == NULL) |
| 1084 | chunk_appendf(out, "Empty\n"); |
| 1085 | else if (LIST_ISEMPTY(&ckchs->ckch_inst)) |
| 1086 | chunk_appendf(out, "Unused\n"); |
| 1087 | else |
| 1088 | chunk_appendf(out, "Used\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1089 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1090 | if (ckchs->ckch->cert == NULL) |
| 1091 | goto end; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1092 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1093 | chain = ckchs->ckch->chain; |
| 1094 | if (chain == NULL) { |
| 1095 | struct issuer_chain *issuer; |
| 1096 | issuer = ssl_get0_issuer_chain(ckchs->ckch->cert); |
| 1097 | if (issuer) { |
| 1098 | chain = issuer->chain; |
| 1099 | chunk_appendf(out, "Chain Filename: "); |
| 1100 | chunk_appendf(out, "%s\n", issuer->path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1101 | } |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1102 | } |
| 1103 | chunk_appendf(out, "Serial: "); |
| 1104 | if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1) |
| 1105 | goto end; |
| 1106 | dump_binary(out, tmp->area, tmp->data); |
| 1107 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1108 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1109 | chunk_appendf(out, "notBefore: "); |
| 1110 | chunk_reset(tmp); |
| 1111 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 1112 | goto end; |
| 1113 | if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0) |
| 1114 | goto end; |
| 1115 | write = BIO_read(bio, tmp->area, tmp->size-1); |
| 1116 | tmp->area[write] = '\0'; |
| 1117 | BIO_free(bio); |
| 1118 | bio = NULL; |
| 1119 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1120 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1121 | chunk_appendf(out, "notAfter: "); |
| 1122 | chunk_reset(tmp); |
| 1123 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 1124 | goto end; |
| 1125 | if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0) |
| 1126 | goto end; |
| 1127 | if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0) |
| 1128 | goto end; |
| 1129 | tmp->area[write] = '\0'; |
| 1130 | BIO_free(bio); |
| 1131 | bio = NULL; |
| 1132 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1133 | |
| 1134 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1135 | chunk_appendf(out, "Subject Alternative Name: "); |
| 1136 | if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1) |
| 1137 | goto end; |
| 1138 | *(out->area + out->data) = '\0'; |
| 1139 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1140 | #endif |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1141 | chunk_reset(tmp); |
| 1142 | chunk_appendf(out, "Algorithm: "); |
| 1143 | if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0) |
| 1144 | goto end; |
| 1145 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1146 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1147 | chunk_reset(tmp); |
| 1148 | chunk_appendf(out, "SHA1 FingerPrint: "); |
| 1149 | if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0) |
| 1150 | goto end; |
| 1151 | tmp->data = len; |
| 1152 | dump_binary(out, tmp->area, tmp->data); |
| 1153 | chunk_appendf(out, "\n"); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1154 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1155 | chunk_appendf(out, "Subject: "); |
| 1156 | if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL) |
| 1157 | goto end; |
| 1158 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1159 | goto end; |
| 1160 | *(tmp->area + tmp->data) = '\0'; |
| 1161 | chunk_appendf(out, "%s\n", tmp->area); |
| 1162 | |
| 1163 | chunk_appendf(out, "Issuer: "); |
| 1164 | if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL) |
| 1165 | goto end; |
| 1166 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1167 | goto end; |
| 1168 | *(tmp->area + tmp->data) = '\0'; |
| 1169 | chunk_appendf(out, "%s\n", tmp->area); |
| 1170 | |
| 1171 | /* Displays subject of each certificate in the chain */ |
| 1172 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1173 | X509 *ca = sk_X509_value(chain, i); |
| 1174 | |
| 1175 | chunk_appendf(out, "Chain Subject: "); |
| 1176 | if ((name = X509_get_subject_name(ca)) == NULL) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1177 | goto end; |
| 1178 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1179 | goto end; |
| 1180 | *(tmp->area + tmp->data) = '\0'; |
| 1181 | chunk_appendf(out, "%s\n", tmp->area); |
| 1182 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1183 | chunk_appendf(out, "Chain Issuer: "); |
| 1184 | if ((name = X509_get_issuer_name(ca)) == NULL) |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1185 | goto end; |
| 1186 | if ((ssl_sock_get_dn_oneline(name, tmp)) == -1) |
| 1187 | goto end; |
| 1188 | *(tmp->area + tmp->data) = '\0'; |
| 1189 | chunk_appendf(out, "%s\n", tmp->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | end: |
| 1193 | if (ci_putchk(si_ic(si), out) == -1) { |
| 1194 | si_rx_room_blk(si); |
| 1195 | goto yield; |
| 1196 | } |
| 1197 | |
| 1198 | end_no_putchk: |
| 1199 | if (bio) |
| 1200 | BIO_free(bio); |
| 1201 | free_trash_chunk(tmp); |
| 1202 | free_trash_chunk(out); |
| 1203 | return 1; |
| 1204 | yield: |
| 1205 | free_trash_chunk(tmp); |
| 1206 | free_trash_chunk(out); |
| 1207 | return 0; /* should come back */ |
| 1208 | } |
| 1209 | |
| 1210 | /* parsing function for 'show ssl cert [certfile]' */ |
| 1211 | static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1212 | { |
| 1213 | struct ckch_store *ckchs; |
| 1214 | |
| 1215 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 1216 | return cli_err(appctx, "Can't allocate memory!\n"); |
| 1217 | |
| 1218 | /* The operations on the CKCH architecture are locked so we can |
| 1219 | * manipulate ckch_store and ckch_inst */ |
| 1220 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1221 | return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n"); |
| 1222 | |
| 1223 | /* check if there is a certificate to lookup */ |
| 1224 | if (*args[3]) { |
| 1225 | if (*args[3] == '*') { |
| 1226 | if (!ckchs_transaction.new_ckchs) |
| 1227 | goto error; |
| 1228 | |
| 1229 | ckchs = ckchs_transaction.new_ckchs; |
| 1230 | |
| 1231 | if (strcmp(args[3] + 1, ckchs->path)) |
| 1232 | goto error; |
| 1233 | |
| 1234 | } else { |
| 1235 | if ((ckchs = ckchs_lookup(args[3])) == NULL) |
| 1236 | goto error; |
| 1237 | |
| 1238 | } |
| 1239 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1240 | appctx->ctx.cli.p0 = ckchs; |
| 1241 | /* use the IO handler that shows details */ |
| 1242 | appctx->io_handler = cli_io_handler_show_cert_detail; |
| 1243 | } |
| 1244 | |
| 1245 | return 0; |
| 1246 | |
| 1247 | error: |
| 1248 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1249 | return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n"); |
| 1250 | } |
| 1251 | |
| 1252 | /* release function of the `set ssl cert' command, free things and unlock the spinlock */ |
| 1253 | static void cli_release_commit_cert(struct appctx *appctx) |
| 1254 | { |
| 1255 | struct ckch_store *new_ckchs; |
| 1256 | |
| 1257 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1258 | |
| 1259 | if (appctx->st2 != SETCERT_ST_FIN) { |
| 1260 | /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */ |
| 1261 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 1262 | |
| 1263 | /* if the allocation failed, we need to free everything from the temporary list */ |
| 1264 | ckch_store_free(new_ckchs); |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | * This function tries to create the new ckch_inst and their SNIs |
| 1270 | */ |
| 1271 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
| 1272 | { |
| 1273 | struct stream_interface *si = appctx->owner; |
| 1274 | int y = 0; |
| 1275 | char *err = NULL; |
| 1276 | int errcode = 0; |
| 1277 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
| 1278 | struct ckch_inst *ckchi, *ckchis; |
| 1279 | struct buffer *trash = alloc_trash_chunk(); |
| 1280 | struct sni_ctx *sc0, *sc0s; |
| 1281 | struct crtlist_entry *entry; |
| 1282 | |
| 1283 | if (trash == NULL) |
| 1284 | goto error; |
| 1285 | |
| 1286 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 1287 | goto error; |
| 1288 | |
| 1289 | while (1) { |
| 1290 | switch (appctx->st2) { |
| 1291 | case SETCERT_ST_INIT: |
| 1292 | /* This state just print the update message */ |
| 1293 | chunk_printf(trash, "Committing %s", ckchs_transaction.path); |
| 1294 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 1295 | si_rx_room_blk(si); |
| 1296 | goto yield; |
| 1297 | } |
| 1298 | appctx->st2 = SETCERT_ST_GEN; |
| 1299 | /* fallthrough */ |
| 1300 | case SETCERT_ST_GEN: |
| 1301 | /* |
| 1302 | * This state generates the ckch instances with their |
| 1303 | * sni_ctxs and SSL_CTX. |
| 1304 | * |
| 1305 | * Since the SSL_CTX generation can be CPU consumer, we |
| 1306 | * yield every 10 instances. |
| 1307 | */ |
| 1308 | |
| 1309 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 1310 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 1311 | |
| 1312 | if (!new_ckchs) |
| 1313 | continue; |
| 1314 | |
| 1315 | /* get the next ckchi to regenerate */ |
| 1316 | ckchi = appctx->ctx.ssl.next_ckchi; |
| 1317 | /* we didn't start yet, set it to the first elem */ |
| 1318 | if (ckchi == NULL) |
| 1319 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 1320 | |
| 1321 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 1322 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 1323 | struct ckch_inst *new_inst; |
| 1324 | char **sni_filter = NULL; |
| 1325 | int fcount = 0; |
| 1326 | |
| 1327 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 1328 | if (y >= 10) { |
| 1329 | /* save the next ckchi to compute */ |
| 1330 | appctx->ctx.ssl.next_ckchi = ckchi; |
| 1331 | goto yield; |
| 1332 | } |
| 1333 | |
| 1334 | if (ckchi->crtlist_entry) { |
| 1335 | sni_filter = ckchi->crtlist_entry->filters; |
| 1336 | fcount = ckchi->crtlist_entry->fcount; |
| 1337 | } |
| 1338 | |
William Lallemand | 95fefa1 | 2020-09-09 12:01:33 +0200 | [diff] [blame] | 1339 | errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1340 | |
| 1341 | if (errcode & ERR_CODE) |
| 1342 | goto error; |
| 1343 | |
| 1344 | /* if the previous ckchi was used as the default */ |
| 1345 | if (ckchi->is_default) |
| 1346 | new_inst->is_default = 1; |
| 1347 | |
William Lallemand | 9f71469 | 2020-12-15 14:57:46 +0100 | [diff] [blame] | 1348 | /* create the link to the crtlist_entry */ |
| 1349 | new_inst->crtlist_entry = ckchi->crtlist_entry; |
| 1350 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1351 | /* we need to initialize the SSL_CTX generated */ |
| 1352 | /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */ |
| 1353 | list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) { |
| 1354 | if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */ |
| 1355 | errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err); |
| 1356 | if (errcode & ERR_CODE) |
| 1357 | goto error; |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | /* display one dot per new instance */ |
| 1363 | chunk_appendf(trash, "."); |
| 1364 | /* link the new ckch_inst to the duplicate */ |
| 1365 | LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 1366 | y++; |
| 1367 | } |
| 1368 | appctx->st2 = SETCERT_ST_INSERT; |
| 1369 | /* fallthrough */ |
| 1370 | case SETCERT_ST_INSERT: |
| 1371 | /* The generation is finished, we can insert everything */ |
| 1372 | |
| 1373 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 1374 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 1375 | |
| 1376 | if (!new_ckchs) |
| 1377 | continue; |
| 1378 | |
| 1379 | /* get the list of crtlist_entry in the old store, and update the pointers to the store */ |
| 1380 | LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry); |
| 1381 | list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) { |
| 1382 | ebpt_delete(&entry->node); |
| 1383 | /* change the ptr and reinsert the node */ |
| 1384 | entry->node.key = new_ckchs; |
| 1385 | ebpt_insert(&entry->crtlist->entries, &entry->node); |
| 1386 | } |
| 1387 | |
William Lallemand | 9f71469 | 2020-12-15 14:57:46 +0100 | [diff] [blame] | 1388 | /* insert the new ckch_insts in the crtlist_entry */ |
| 1389 | list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) { |
| 1390 | if (ckchi->crtlist_entry) |
| 1391 | LIST_ADD(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry); |
| 1392 | } |
| 1393 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1394 | /* First, we insert every new SNIs in the trees, also replace the default_ctx */ |
| 1395 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 1396 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 1397 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 1398 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 1399 | } |
| 1400 | |
| 1401 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 1402 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
| 1403 | struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf; |
| 1404 | |
| 1405 | HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock); |
| 1406 | ckch_inst_free(ckchi); |
| 1407 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock); |
| 1408 | } |
| 1409 | |
| 1410 | /* Replace the old ckchs by the new one */ |
| 1411 | ckch_store_free(old_ckchs); |
| 1412 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
| 1413 | appctx->st2 = SETCERT_ST_FIN; |
| 1414 | /* fallthrough */ |
| 1415 | case SETCERT_ST_FIN: |
| 1416 | /* we achieved the transaction, we can set everything to NULL */ |
| 1417 | free(ckchs_transaction.path); |
| 1418 | ckchs_transaction.path = NULL; |
| 1419 | ckchs_transaction.new_ckchs = NULL; |
| 1420 | ckchs_transaction.old_ckchs = NULL; |
| 1421 | goto end; |
| 1422 | } |
| 1423 | } |
| 1424 | end: |
| 1425 | |
| 1426 | chunk_appendf(trash, "\n"); |
| 1427 | if (errcode & ERR_WARN) |
| 1428 | chunk_appendf(trash, "%s", err); |
| 1429 | chunk_appendf(trash, "Success!\n"); |
| 1430 | if (ci_putchk(si_ic(si), trash) == -1) |
| 1431 | si_rx_room_blk(si); |
| 1432 | free_trash_chunk(trash); |
| 1433 | /* success: call the release function and don't come back */ |
| 1434 | return 1; |
| 1435 | yield: |
| 1436 | /* store the state */ |
| 1437 | if (ci_putchk(si_ic(si), trash) == -1) |
| 1438 | si_rx_room_blk(si); |
| 1439 | free_trash_chunk(trash); |
| 1440 | si_rx_endp_more(si); /* let's come back later */ |
| 1441 | return 0; /* should come back */ |
| 1442 | |
| 1443 | error: |
| 1444 | /* spin unlock and free are done in the release function */ |
| 1445 | if (trash) { |
| 1446 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 1447 | if (ci_putchk(si_ic(si), trash) == -1) |
| 1448 | si_rx_room_blk(si); |
| 1449 | free_trash_chunk(trash); |
| 1450 | } |
| 1451 | /* error: call the release function and don't come back */ |
| 1452 | return 1; |
| 1453 | } |
| 1454 | |
| 1455 | /* |
| 1456 | * Parsing function of 'commit ssl cert' |
| 1457 | */ |
| 1458 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1459 | { |
| 1460 | char *err = NULL; |
| 1461 | |
| 1462 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1463 | return 1; |
| 1464 | |
| 1465 | if (!*args[3]) |
| 1466 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 1467 | |
| 1468 | /* The operations on the CKCH architecture are locked so we can |
| 1469 | * manipulate ckch_store and ckch_inst */ |
| 1470 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1471 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 1472 | |
| 1473 | if (!ckchs_transaction.path) { |
| 1474 | memprintf(&err, "No ongoing transaction! !\n"); |
| 1475 | goto error; |
| 1476 | } |
| 1477 | |
| 1478 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 1479 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 1480 | goto error; |
| 1481 | } |
| 1482 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1483 | /* if a certificate is here, a private key must be here too */ |
| 1484 | if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) { |
| 1485 | memprintf(&err, "The transaction must contain at least a certificate and a private key!\n"); |
| 1486 | goto error; |
| 1487 | } |
William Lallemand | a941952 | 2020-06-24 16:26:41 +0200 | [diff] [blame] | 1488 | |
William Lallemand | 5685ccf | 2020-09-16 16:12:25 +0200 | [diff] [blame] | 1489 | if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) { |
| 1490 | memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path); |
| 1491 | goto error; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /* init the appctx structure */ |
| 1495 | appctx->st2 = SETCERT_ST_INIT; |
| 1496 | appctx->ctx.ssl.next_ckchi = NULL; |
| 1497 | appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs; |
| 1498 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs; |
| 1499 | |
| 1500 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 1501 | return 0; |
| 1502 | |
| 1503 | error: |
| 1504 | |
| 1505 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1506 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 1507 | |
| 1508 | return cli_dynerr(appctx, err); |
| 1509 | } |
| 1510 | |
| 1511 | |
| 1512 | |
| 1513 | |
| 1514 | /* |
| 1515 | * Parsing function of `set ssl cert`, it updates or creates a temporary ckch. |
| 1516 | */ |
| 1517 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1518 | { |
| 1519 | struct ckch_store *new_ckchs = NULL; |
| 1520 | struct ckch_store *old_ckchs = NULL; |
| 1521 | char *err = NULL; |
| 1522 | int i; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1523 | int errcode = 0; |
| 1524 | char *end; |
| 1525 | int type = CERT_TYPE_PEM; |
| 1526 | struct cert_key_and_chain *ckch; |
| 1527 | struct buffer *buf; |
| 1528 | |
| 1529 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1530 | return 1; |
| 1531 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1532 | if (!*args[3] || !payload) |
| 1533 | return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n"); |
| 1534 | |
| 1535 | /* The operations on the CKCH architecture are locked so we can |
| 1536 | * manipulate ckch_store and ckch_inst */ |
| 1537 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1538 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 1539 | |
William Lallemand | f41d4db | 2021-05-04 16:17:27 +0200 | [diff] [blame] | 1540 | if ((buf = alloc_trash_chunk()) == NULL) { |
| 1541 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 1542 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1543 | goto end; |
| 1544 | } |
William Lallemand | e5ff4ad | 2020-06-08 09:40:37 +0200 | [diff] [blame] | 1545 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1546 | if (!chunk_strcpy(buf, args[3])) { |
| 1547 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 1548 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1549 | goto end; |
| 1550 | } |
| 1551 | |
| 1552 | /* check which type of file we want to update */ |
| 1553 | for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) { |
| 1554 | end = strrchr(buf->area, '.'); |
| 1555 | if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) { |
| 1556 | *end = '\0'; |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 1557 | buf->data = strlen(buf->area); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1558 | type = cert_exts[i].type; |
| 1559 | break; |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | appctx->ctx.ssl.old_ckchs = NULL; |
| 1564 | appctx->ctx.ssl.new_ckchs = NULL; |
| 1565 | |
| 1566 | /* if there is an ongoing transaction */ |
| 1567 | if (ckchs_transaction.path) { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1568 | /* if there is an ongoing transaction, check if this is the same file */ |
| 1569 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 1570 | /* we didn't find the transaction, must try more cases below */ |
| 1571 | |
| 1572 | /* if the del-ext option is activated we should try to take a look at a ".crt" too. */ |
| 1573 | if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) { |
| 1574 | if (!chunk_strcat(buf, ".crt")) { |
| 1575 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 1576 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1577 | goto end; |
| 1578 | } |
| 1579 | |
| 1580 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 1581 | /* remove .crt of the error message */ |
| 1582 | *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0'; |
| 1583 | b_sub(buf, strlen(".crt")); |
| 1584 | |
| 1585 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 1586 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1587 | goto end; |
| 1588 | } |
| 1589 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs; |
| 1593 | |
| 1594 | } else { |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1595 | |
William Lallemand | 95fefa1 | 2020-09-09 12:01:33 +0200 | [diff] [blame] | 1596 | /* lookup for the certificate in the tree */ |
| 1597 | appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area); |
William Lallemand | 089c138 | 2020-10-23 17:35:12 +0200 | [diff] [blame] | 1598 | |
| 1599 | if (!appctx->ctx.ssl.old_ckchs) { |
| 1600 | /* if the del-ext option is activated we should try to take a look at a ".crt" too. */ |
| 1601 | if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) { |
| 1602 | if (!chunk_strcat(buf, ".crt")) { |
| 1603 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 1604 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1605 | goto end; |
| 1606 | } |
| 1607 | appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area); |
| 1608 | } |
| 1609 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | if (!appctx->ctx.ssl.old_ckchs) { |
| 1613 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", |
| 1614 | err ? err : ""); |
| 1615 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1616 | goto end; |
| 1617 | } |
| 1618 | |
| 1619 | if (!appctx->ctx.ssl.path) { |
| 1620 | /* this is a new transaction, set the path of the transaction */ |
| 1621 | appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path); |
| 1622 | if (!appctx->ctx.ssl.path) { |
| 1623 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 1624 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1625 | goto end; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 1630 | |
| 1631 | /* duplicate the ckch store */ |
| 1632 | new_ckchs = ckchs_dup(old_ckchs); |
| 1633 | if (!new_ckchs) { |
| 1634 | memprintf(&err, "%sCannot allocate memory!\n", |
| 1635 | err ? err : ""); |
| 1636 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1637 | goto end; |
| 1638 | } |
| 1639 | |
William Lallemand | 95fefa1 | 2020-09-09 12:01:33 +0200 | [diff] [blame] | 1640 | ckch = new_ckchs->ckch; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1641 | |
| 1642 | /* appply the change on the duplicate */ |
| 1643 | if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) { |
| 1644 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 1645 | errcode |= ERR_ALERT | ERR_FATAL; |
| 1646 | goto end; |
| 1647 | } |
| 1648 | |
| 1649 | appctx->ctx.ssl.new_ckchs = new_ckchs; |
| 1650 | |
| 1651 | /* we succeed, we can save the ckchs in the transaction */ |
| 1652 | |
| 1653 | /* if there wasn't a transaction, update the old ckchs */ |
| 1654 | if (!ckchs_transaction.old_ckchs) { |
| 1655 | ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 1656 | ckchs_transaction.path = appctx->ctx.ssl.path; |
| 1657 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 1658 | } else { |
| 1659 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 1660 | |
| 1661 | } |
| 1662 | |
| 1663 | /* free the previous ckchs if there was a transaction */ |
| 1664 | ckch_store_free(ckchs_transaction.new_ckchs); |
| 1665 | |
| 1666 | ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 1667 | |
| 1668 | |
| 1669 | /* creates the SNI ctxs later in the IO handler */ |
| 1670 | |
| 1671 | end: |
| 1672 | free_trash_chunk(buf); |
| 1673 | |
| 1674 | if (errcode & ERR_CODE) { |
| 1675 | |
| 1676 | ckch_store_free(appctx->ctx.ssl.new_ckchs); |
| 1677 | appctx->ctx.ssl.new_ckchs = NULL; |
| 1678 | |
| 1679 | appctx->ctx.ssl.old_ckchs = NULL; |
| 1680 | |
| 1681 | free(appctx->ctx.ssl.path); |
| 1682 | appctx->ctx.ssl.path = NULL; |
| 1683 | |
| 1684 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1685 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
| 1686 | } else { |
| 1687 | |
| 1688 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1689 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 1690 | } |
| 1691 | /* TODO: handle the ERR_WARN which are not handled because of the io_handler */ |
| 1692 | } |
| 1693 | |
| 1694 | /* parsing function of 'abort ssl cert' */ |
| 1695 | static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1696 | { |
| 1697 | char *err = NULL; |
| 1698 | |
| 1699 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1700 | return 1; |
| 1701 | |
| 1702 | if (!*args[3]) |
| 1703 | return cli_err(appctx, "'abort ssl cert' expects a filename\n"); |
| 1704 | |
| 1705 | /* The operations on the CKCH architecture are locked so we can |
| 1706 | * manipulate ckch_store and ckch_inst */ |
| 1707 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1708 | return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n"); |
| 1709 | |
| 1710 | if (!ckchs_transaction.path) { |
| 1711 | memprintf(&err, "No ongoing transaction!\n"); |
| 1712 | goto error; |
| 1713 | } |
| 1714 | |
| 1715 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 1716 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]); |
| 1717 | goto error; |
| 1718 | } |
| 1719 | |
| 1720 | /* Only free the ckchs there, because the SNI and instances were not generated yet */ |
| 1721 | ckch_store_free(ckchs_transaction.new_ckchs); |
| 1722 | ckchs_transaction.new_ckchs = NULL; |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1723 | ckchs_transaction.old_ckchs = NULL; |
| 1724 | free(ckchs_transaction.path); |
| 1725 | ckchs_transaction.path = NULL; |
| 1726 | |
| 1727 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1728 | |
| 1729 | err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]); |
| 1730 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 1731 | |
| 1732 | error: |
| 1733 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1734 | |
| 1735 | return cli_dynerr(appctx, err); |
| 1736 | } |
| 1737 | |
| 1738 | /* parsing function of 'new ssl cert' */ |
| 1739 | static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1740 | { |
| 1741 | struct ckch_store *store; |
| 1742 | char *err = NULL; |
| 1743 | char *path; |
| 1744 | |
| 1745 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1746 | return 1; |
| 1747 | |
| 1748 | if (!*args[3]) |
| 1749 | return cli_err(appctx, "'new ssl cert' expects a filename\n"); |
| 1750 | |
| 1751 | path = args[3]; |
| 1752 | |
| 1753 | /* The operations on the CKCH architecture are locked so we can |
| 1754 | * manipulate ckch_store and ckch_inst */ |
| 1755 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1756 | return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n"); |
| 1757 | |
| 1758 | store = ckchs_lookup(path); |
| 1759 | if (store != NULL) { |
| 1760 | memprintf(&err, "Certificate '%s' already exists!\n", path); |
| 1761 | store = NULL; /* we don't want to free it */ |
| 1762 | goto error; |
| 1763 | } |
| 1764 | /* we won't support multi-certificate bundle here */ |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 1765 | store = ckch_store_new(path); |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1766 | if (!store) { |
| 1767 | memprintf(&err, "unable to allocate memory.\n"); |
| 1768 | goto error; |
| 1769 | } |
| 1770 | |
| 1771 | /* insert into the ckchs tree */ |
| 1772 | ebst_insert(&ckchs_tree, &store->node); |
| 1773 | memprintf(&err, "New empty certificate store '%s'!\n", args[3]); |
| 1774 | |
| 1775 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1776 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 1777 | error: |
| 1778 | free(store); |
| 1779 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1780 | return cli_dynerr(appctx, err); |
| 1781 | } |
| 1782 | |
| 1783 | /* parsing function of 'del ssl cert' */ |
| 1784 | static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 1785 | { |
| 1786 | struct ckch_store *store; |
| 1787 | char *err = NULL; |
| 1788 | char *filename; |
| 1789 | |
| 1790 | if (!cli_has_level(appctx, ACCESS_LVL_ADMIN)) |
| 1791 | return 1; |
| 1792 | |
| 1793 | if (!*args[3]) |
| 1794 | return cli_err(appctx, "'del ssl cert' expects a certificate name\n"); |
| 1795 | |
| 1796 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 1797 | return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n"); |
| 1798 | |
| 1799 | filename = args[3]; |
| 1800 | |
| 1801 | store = ckchs_lookup(filename); |
| 1802 | if (store == NULL) { |
| 1803 | memprintf(&err, "certificate '%s' doesn't exist!\n", filename); |
| 1804 | goto error; |
| 1805 | } |
| 1806 | if (!LIST_ISEMPTY(&store->ckch_inst)) { |
| 1807 | memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename); |
| 1808 | goto error; |
| 1809 | } |
| 1810 | |
| 1811 | ebmb_delete(&store->node); |
| 1812 | ckch_store_free(store); |
| 1813 | |
| 1814 | memprintf(&err, "Certificate '%s' deleted!\n", filename); |
| 1815 | |
| 1816 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1817 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 1818 | |
| 1819 | error: |
| 1820 | memprintf(&err, "Can't remove the certificate: %s\n", err ? err : ""); |
| 1821 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 1822 | return cli_dynerr(appctx, err); |
| 1823 | } |
| 1824 | |
William Lallemand | ee8530c | 2020-06-23 18:19:42 +0200 | [diff] [blame] | 1825 | void ckch_deinit() |
| 1826 | { |
| 1827 | struct eb_node *node, *next; |
| 1828 | struct ckch_store *store; |
| 1829 | |
| 1830 | node = eb_first(&ckchs_tree); |
| 1831 | while (node) { |
| 1832 | next = eb_next(node); |
| 1833 | store = ebmb_entry(node, struct ckch_store, node); |
| 1834 | ckch_store_free(store); |
| 1835 | node = next; |
| 1836 | } |
| 1837 | } |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 1838 | |
| 1839 | /* register cli keywords */ |
| 1840 | static struct cli_kw_list cli_kws = {{ },{ |
| 1841 | { { "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 }, |
| 1842 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 1843 | { { "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 }, |
| 1844 | { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL }, |
| 1845 | { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL }, |
| 1846 | { { "show", "ssl", "cert", NULL }, "show ssl cert [<certfile>] : display the SSL certificates used in memory, or the details of a <certfile>", cli_parse_show_cert, cli_io_handler_show_cert, cli_release_show_cert }, |
| 1847 | { { NULL }, NULL, NULL, NULL } |
| 1848 | }}; |
| 1849 | |
| 1850 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
| 1851 | |