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