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