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