blob: 071f45aa9a8273cd4fb25b753a92484df8873dbc [file] [log] [blame]
William Lallemand03c331c2020-05-13 10:10:01 +02001/*
2 *
3 * Copyright (C) 2020 HAProxy Technologies, William Lallemand <wlallemand@haproxy.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 */
11
12#define _GNU_SOURCE
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020019#include <syslog.h>
William Lallemand03c331c2020-05-13 10:10:01 +020020#include <unistd.h>
21
22#include <sys/stat.h>
23#include <sys/types.h>
24
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <import/ebsttree.h>
26
Willy Tarreau8d366972020-05-27 16:10:29 +020027#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020028#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020029#include <haproxy/cli.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020030#include <haproxy/errors.h>
Willy Tarreau47d7f902020-06-04 14:25:47 +020031#include <haproxy/ssl_ckch.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020032#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020033#include <haproxy/ssl_utils.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020035#include <haproxy/tools.h>
William Lallemand03c331c2020-05-13 10:10:01 +020036
William Lallemandda8584c2020-05-14 10:14:37 +020037/* Uncommitted CKCH transaction */
38
39static struct {
40 struct ckch_store *new_ckchs;
41 struct ckch_store *old_ckchs;
42 char *path;
43} ckchs_transaction;
44
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +010045/* Uncommitted CA file transaction */
46
47static struct {
48 struct cafile_entry *old_cafile_entry;
49 struct cafile_entry *new_cafile_entry;
50 char *path;
51} cafile_transaction;
William Lallemandda8584c2020-05-14 10:14:37 +020052
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +020053/* Uncommitted CRL file transaction */
54
55static struct {
56 struct cafile_entry *old_crlfile_entry;
57 struct cafile_entry *new_crlfile_entry;
58 char *path;
59} crlfile_transaction;
60
William Lallemand03c331c2020-05-13 10:10:01 +020061
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +010062
William Lallemand03c331c2020-05-13 10:10:01 +020063/******************** cert_key_and_chain functions *************************
64 * These are the functions that fills a cert_key_and_chain structure. For the
65 * functions filling a SSL_CTX from a cert_key_and_chain, see ssl_sock.c
66 */
67
68/*
69 * Try to parse Signed Certificate Timestamp List structure. This function
70 * makes only basic test if the data seems like SCTL. No signature validation
71 * is performed.
72 */
73static int ssl_sock_parse_sctl(struct buffer *sctl)
74{
75 int ret = 1;
76 int len, pos, sct_len;
77 unsigned char *data;
78
79 if (sctl->data < 2)
80 goto out;
81
82 data = (unsigned char *) sctl->area;
83 len = (data[0] << 8) | data[1];
84
85 if (len + 2 != sctl->data)
86 goto out;
87
88 data = data + 2;
89 pos = 0;
90 while (pos < len) {
91 if (len - pos < 2)
92 goto out;
93
94 sct_len = (data[pos] << 8) | data[pos + 1];
95 if (pos + sct_len + 2 > len)
96 goto out;
97
98 pos += sct_len + 2;
99 }
100
101 ret = 0;
102
103out:
104 return ret;
105}
106
107/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
108 * It fills the ckch->sctl buffer
109 * return 0 on success or != 0 on failure */
110int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
111{
112 int fd = -1;
113 int r = 0;
114 int ret = 1;
115 struct buffer tmp;
116 struct buffer *src;
117 struct buffer *sctl;
118
119 if (buf) {
William Lallemand8d673942021-01-27 14:58:51 +0100120 chunk_initstr(&tmp, buf);
William Lallemand03c331c2020-05-13 10:10:01 +0200121 src = &tmp;
122 } else {
123 fd = open(sctl_path, O_RDONLY);
124 if (fd == -1)
125 goto end;
126
127 trash.data = 0;
128 while (trash.data < trash.size) {
129 r = read(fd, trash.area + trash.data, trash.size - trash.data);
130 if (r < 0) {
131 if (errno == EINTR)
132 continue;
133 goto end;
134 }
135 else if (r == 0) {
136 break;
137 }
138 trash.data += r;
139 }
140 src = &trash;
141 }
142
143 ret = ssl_sock_parse_sctl(src);
144 if (ret)
145 goto end;
146
147 sctl = calloc(1, sizeof(*sctl));
148 if (!chunk_dup(sctl, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100149 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200150 goto end;
151 }
152 /* no error, fill ckch with new context, old context must be free */
153 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100154 ha_free(&ckch->sctl->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200155 free(ckch->sctl);
156 }
157 ckch->sctl = sctl;
158 ret = 0;
159end:
160 if (fd != -1)
161 close(fd);
162
163 return ret;
164}
165
166#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
167/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500168 * This function load the OCSP Response in DER format contained in file at
William Lallemand03c331c2020-05-13 10:10:01 +0200169 * path 'ocsp_path' or base64 in a buffer <buf>
170 *
171 * Returns 0 on success, 1 in error case.
172 */
173int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
174{
175 int fd = -1;
176 int r = 0;
177 int ret = 1;
178 struct buffer *ocsp_response;
179 struct buffer *src = NULL;
180
181 if (buf) {
182 int i, j;
183 /* if it's from a buffer it will be base64 */
184
185 /* remove \r and \n from the payload */
186 for (i = 0, j = 0; buf[i]; i++) {
187 if (buf[i] == '\r' || buf[i] == '\n')
188 continue;
189 buf[j++] = buf[i];
190 }
191 buf[j] = 0;
192
193 ret = base64dec(buf, j, trash.area, trash.size);
194 if (ret < 0) {
195 memprintf(err, "Error reading OCSP response in base64 format");
196 goto end;
197 }
198 trash.data = ret;
199 src = &trash;
200 } else {
201 fd = open(ocsp_path, O_RDONLY);
202 if (fd == -1) {
203 memprintf(err, "Error opening OCSP response file");
204 goto end;
205 }
206
207 trash.data = 0;
208 while (trash.data < trash.size) {
209 r = read(fd, trash.area + trash.data, trash.size - trash.data);
210 if (r < 0) {
211 if (errno == EINTR)
212 continue;
213
214 memprintf(err, "Error reading OCSP response from file");
215 goto end;
216 }
217 else if (r == 0) {
218 break;
219 }
220 trash.data += r;
221 }
222 close(fd);
223 fd = -1;
224 src = &trash;
225 }
226
227 ocsp_response = calloc(1, sizeof(*ocsp_response));
228 if (!chunk_dup(ocsp_response, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100229 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200230 goto end;
231 }
232 /* no error, fill ckch with new context, old context must be free */
233 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100234 ha_free(&ckch->ocsp_response->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200235 free(ckch->ocsp_response);
236 }
237 ckch->ocsp_response = ocsp_response;
238 ret = 0;
239end:
240 if (fd != -1)
241 close(fd);
242
243 return ret;
244}
245#endif
246
247/*
248 * Try to load in a ckch every files related to a ckch.
249 * (PEM, sctl, ocsp, issuer etc.)
250 *
251 * This function is only used to load files during the configuration parsing,
252 * it is not used with the CLI.
253 *
254 * This allows us to carry the contents of the file without having to read the
255 * file multiple times. The caller must call
256 * ssl_sock_free_cert_key_and_chain_contents.
257 *
258 * returns:
259 * 0 on Success
260 * 1 on SSL Failure
261 */
262int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
263{
William Lallemand8e8581e2020-10-20 17:36:46 +0200264 struct buffer *fp = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200265 int ret = 1;
266
267 /* try to load the PEM */
268 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
269 goto end;
270 }
271
William Lallemand8e8581e2020-10-20 17:36:46 +0200272 fp = alloc_trash_chunk();
273 if (!fp) {
274 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
275 goto end;
276 }
277
278 if (!chunk_strcpy(fp, path) || (b_data(fp) > MAXPATHLEN)) {
279 memprintf(err, "%s '%s' filename too long'.\n",
280 err && *err ? *err : "", fp->area);
281 ret = 1;
282 goto end;
283 }
284
William Lallemand089c1382020-10-23 17:35:12 +0200285 /* remove the ".crt" extension */
William Lallemand8e8581e2020-10-20 17:36:46 +0200286 if (global_ssl.extra_files_noext) {
287 char *ext;
288
289 /* look for the extension */
290 if ((ext = strrchr(fp->area, '.'))) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200291
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100292 if (strcmp(ext, ".crt") == 0) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200293 *ext = '\0';
William Lallemand089c1382020-10-23 17:35:12 +0200294 fp->data = strlen(fp->area);
295 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200296 }
297
298 }
299
William Lallemand03c331c2020-05-13 10:10:01 +0200300 /* try to load an external private key if it wasn't in the PEM */
301 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200302 struct stat st;
303
William Lallemand8e8581e2020-10-20 17:36:46 +0200304
305 if (!chunk_strcat(fp, ".key") || (b_data(fp) > MAXPATHLEN)) {
306 memprintf(err, "%s '%s' filename too long'.\n",
307 err && *err ? *err : "", fp->area);
308 ret = 1;
309 goto end;
310 }
311
312 if (stat(fp->area, &st) == 0) {
313 if (ssl_sock_load_key_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200314 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200315 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200316 goto end;
317 }
318 }
William Lallemand03c331c2020-05-13 10:10:01 +0200319
William Lallemand8e8581e2020-10-20 17:36:46 +0200320 if (ckch->key == NULL) {
321 memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area);
322 goto end;
323 }
324 /* remove the added extension */
325 *(fp->area + fp->data - strlen(".key")) = '\0';
326 b_sub(fp, strlen(".key"));
William Lallemand03c331c2020-05-13 10:10:01 +0200327 }
328
329 if (!X509_check_private_key(ckch->cert, ckch->key)) {
330 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
331 err && *err ? *err : "", path);
332 goto end;
333 }
334
Ilya Shipitsinc47d6762021-02-13 11:45:33 +0500335#ifdef HAVE_SSL_SCTL
William Lallemand03c331c2020-05-13 10:10:01 +0200336 /* try to load the sctl file */
337 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemand03c331c2020-05-13 10:10:01 +0200338 struct stat st;
339
William Lallemand8e8581e2020-10-20 17:36:46 +0200340 if (!chunk_strcat(fp, ".sctl") || b_data(fp) > MAXPATHLEN) {
341 memprintf(err, "%s '%s' filename too long'.\n",
342 err && *err ? *err : "", fp->area);
343 ret = 1;
344 goto end;
345 }
346
347 if (stat(fp->area, &st) == 0) {
348 if (ssl_sock_load_sctl_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200349 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200350 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200351 ret = 1;
352 goto end;
353 }
354 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200355 /* remove the added extension */
356 *(fp->area + fp->data - strlen(".sctl")) = '\0';
357 b_sub(fp, strlen(".sctl"));
William Lallemand03c331c2020-05-13 10:10:01 +0200358 }
359#endif
360
361 /* try to load an ocsp response file */
362 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand03c331c2020-05-13 10:10:01 +0200363 struct stat st;
364
William Lallemand8e8581e2020-10-20 17:36:46 +0200365 if (!chunk_strcat(fp, ".ocsp") || b_data(fp) > MAXPATHLEN) {
366 memprintf(err, "%s '%s' filename too long'.\n",
367 err && *err ? *err : "", fp->area);
368 ret = 1;
369 goto end;
370 }
371
372 if (stat(fp->area, &st) == 0) {
373 if (ssl_sock_load_ocsp_response_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200374 ret = 1;
375 goto end;
376 }
377 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200378 /* remove the added extension */
379 *(fp->area + fp->data - strlen(".ocsp")) = '\0';
380 b_sub(fp, strlen(".ocsp"));
William Lallemand03c331c2020-05-13 10:10:01 +0200381 }
382
383#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
384 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
385 /* if no issuer was found, try to load an issuer from the .issuer */
386 if (!ckch->ocsp_issuer) {
387 struct stat st;
William Lallemand8e8581e2020-10-20 17:36:46 +0200388
389 if (!chunk_strcat(fp, ".issuer") || b_data(fp) > MAXPATHLEN) {
390 memprintf(err, "%s '%s' filename too long'.\n",
391 err && *err ? *err : "", fp->area);
392 ret = 1;
393 goto end;
394 }
William Lallemand03c331c2020-05-13 10:10:01 +0200395
William Lallemand8e8581e2020-10-20 17:36:46 +0200396 if (stat(fp->area, &st) == 0) {
397 if (ssl_sock_load_issuer_file_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200398 ret = 1;
399 goto end;
400 }
401
402 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
403 memprintf(err, "%s '%s' is not an issuer'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200404 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200405 ret = 1;
406 goto end;
407 }
408 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200409 /* remove the added extension */
410 *(fp->area + fp->data - strlen(".issuer")) = '\0';
411 b_sub(fp, strlen(".issuer"));
William Lallemand03c331c2020-05-13 10:10:01 +0200412 }
413 }
414#endif
415
416 ret = 0;
417
418end:
419
420 ERR_clear_error();
421
422 /* Something went wrong in one of the reads */
423 if (ret != 0)
424 ssl_sock_free_cert_key_and_chain_contents(ckch);
425
William Lallemand8e8581e2020-10-20 17:36:46 +0200426 free_trash_chunk(fp);
427
William Lallemand03c331c2020-05-13 10:10:01 +0200428 return ret;
429}
430
431/*
432 * Try to load a private key file from a <path> or a buffer <buf>
433 *
434 * If it failed you should not attempt to use the ckch but free it.
435 *
436 * Return 0 on success or != 0 on failure
437 */
438int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
439{
440 BIO *in = NULL;
441 int ret = 1;
442 EVP_PKEY *key = NULL;
443
444 if (buf) {
445 /* reading from a buffer */
446 in = BIO_new_mem_buf(buf, -1);
447 if (in == NULL) {
448 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
449 goto end;
450 }
451
452 } else {
453 /* reading from a file */
454 in = BIO_new(BIO_s_file());
455 if (in == NULL)
456 goto end;
457
458 if (BIO_read_filename(in, path) <= 0)
459 goto end;
460 }
461
462 /* Read Private Key */
463 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
464 if (key == NULL) {
465 memprintf(err, "%sunable to load private key from file '%s'.\n",
466 err && *err ? *err : "", path);
467 goto end;
468 }
469
470 ret = 0;
471
472 SWAP(ckch->key, key);
473
474end:
475
476 ERR_clear_error();
477 if (in)
478 BIO_free(in);
479 if (key)
480 EVP_PKEY_free(key);
481
482 return ret;
483}
484
485/*
486 * Try to load a PEM file from a <path> or a buffer <buf>
487 * The PEM must contain at least a Certificate,
488 * It could contain a DH, a certificate chain and a PrivateKey.
489 *
490 * If it failed you should not attempt to use the ckch but free it.
491 *
492 * Return 0 on success or != 0 on failure
493 */
494int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
495{
496 BIO *in = NULL;
497 int ret = 1;
498 X509 *ca;
499 X509 *cert = NULL;
500 EVP_PKEY *key = NULL;
501 DH *dh = NULL;
502 STACK_OF(X509) *chain = NULL;
503
504 if (buf) {
505 /* reading from a buffer */
506 in = BIO_new_mem_buf(buf, -1);
507 if (in == NULL) {
508 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
509 goto end;
510 }
511
512 } else {
513 /* reading from a file */
514 in = BIO_new(BIO_s_file());
515 if (in == NULL) {
516 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
517 goto end;
518 }
519
520 if (BIO_read_filename(in, path) <= 0) {
521 memprintf(err, "%scannot open the file '%s'.\n",
522 err && *err ? *err : "", path);
523 goto end;
524 }
525 }
526
527 /* Read Private Key */
528 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
529 /* no need to check for errors here, because the private key could be loaded later */
530
531#ifndef OPENSSL_NO_DH
532 /* Seek back to beginning of file */
533 if (BIO_reset(in) == -1) {
534 memprintf(err, "%san error occurred while reading the file '%s'.\n",
535 err && *err ? *err : "", path);
536 goto end;
537 }
538
539 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
540 /* no need to return an error there, dh is not mandatory */
541#endif
542
543 /* Seek back to beginning of file */
544 if (BIO_reset(in) == -1) {
545 memprintf(err, "%san error occurred while reading the file '%s'.\n",
546 err && *err ? *err : "", path);
547 goto end;
548 }
549
550 /* Read Certificate */
551 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
552 if (cert == NULL) {
553 memprintf(err, "%sunable to load certificate from file '%s'.\n",
554 err && *err ? *err : "", path);
555 goto end;
556 }
557
558 /* Look for a Certificate Chain */
559 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
560 if (chain == NULL)
561 chain = sk_X509_new_null();
562 if (!sk_X509_push(chain, ca)) {
563 X509_free(ca);
564 goto end;
565 }
566 }
567
568 ret = ERR_get_error();
569 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
570 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
571 err && *err ? *err : "", path);
572 goto end;
573 }
574
575 /* once it loaded the PEM, it should remove everything else in the ckch */
576 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100577 ha_free(&ckch->ocsp_response->area);
578 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200579 }
580
581 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100582 ha_free(&ckch->sctl->area);
583 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200584 }
585
586 if (ckch->ocsp_issuer) {
587 X509_free(ckch->ocsp_issuer);
588 ckch->ocsp_issuer = NULL;
589 }
590
591 /* no error, fill ckch with new context, old context will be free at end: */
592 SWAP(ckch->key, key);
593 SWAP(ckch->dh, dh);
594 SWAP(ckch->cert, cert);
595 SWAP(ckch->chain, chain);
596
597 ret = 0;
598
599end:
600
601 ERR_clear_error();
602 if (in)
603 BIO_free(in);
604 if (key)
605 EVP_PKEY_free(key);
606 if (dh)
607 DH_free(dh);
608 if (cert)
609 X509_free(cert);
610 if (chain)
611 sk_X509_pop_free(chain, X509_free);
612
613 return ret;
614}
615
616/* Frees the contents of a cert_key_and_chain
617 */
618void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
619{
620 if (!ckch)
621 return;
622
623 /* Free the certificate and set pointer to NULL */
624 if (ckch->cert)
625 X509_free(ckch->cert);
626 ckch->cert = NULL;
627
628 /* Free the key and set pointer to NULL */
629 if (ckch->key)
630 EVP_PKEY_free(ckch->key);
631 ckch->key = NULL;
632
633 /* Free each certificate in the chain */
634 if (ckch->chain)
635 sk_X509_pop_free(ckch->chain, X509_free);
636 ckch->chain = NULL;
637
638 if (ckch->dh)
639 DH_free(ckch->dh);
640 ckch->dh = NULL;
641
642 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100643 ha_free(&ckch->sctl->area);
644 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200645 }
646
647 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100648 ha_free(&ckch->ocsp_response->area);
649 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200650 }
651
652 if (ckch->ocsp_issuer)
653 X509_free(ckch->ocsp_issuer);
654 ckch->ocsp_issuer = NULL;
655}
656
657/*
658 *
659 * This function copy a cert_key_and_chain in memory
660 *
661 * It's used to try to apply changes on a ckch before committing them, because
662 * most of the time it's not possible to revert those changes
663 *
664 * Return a the dst or NULL
665 */
666struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
667 struct cert_key_and_chain *dst)
668{
William Lallemand6c096142021-02-23 14:45:45 +0100669 if (!src || !dst)
670 return NULL;
671
William Lallemand03c331c2020-05-13 10:10:01 +0200672 if (src->cert) {
673 dst->cert = src->cert;
674 X509_up_ref(src->cert);
675 }
676
677 if (src->key) {
678 dst->key = src->key;
679 EVP_PKEY_up_ref(src->key);
680 }
681
682 if (src->chain) {
683 dst->chain = X509_chain_up_ref(src->chain);
684 }
685
686 if (src->dh) {
687 DH_up_ref(src->dh);
688 dst->dh = src->dh;
689 }
690
691 if (src->sctl) {
692 struct buffer *sctl;
693
694 sctl = calloc(1, sizeof(*sctl));
695 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100696 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200697 goto error;
698 }
699 dst->sctl = sctl;
700 }
701
702 if (src->ocsp_response) {
703 struct buffer *ocsp_response;
704
705 ocsp_response = calloc(1, sizeof(*ocsp_response));
706 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100707 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200708 goto error;
709 }
710 dst->ocsp_response = ocsp_response;
711 }
712
713 if (src->ocsp_issuer) {
714 X509_up_ref(src->ocsp_issuer);
715 dst->ocsp_issuer = src->ocsp_issuer;
716 }
717
718 return dst;
719
720error:
721
722 /* free everything */
723 ssl_sock_free_cert_key_and_chain_contents(dst);
724
725 return NULL;
726}
727
728/*
729 * return 0 on success or != 0 on failure
730 */
731int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
732{
733 int ret = 1;
734 BIO *in = NULL;
735 X509 *issuer;
736
737 if (buf) {
738 /* reading from a buffer */
739 in = BIO_new_mem_buf(buf, -1);
740 if (in == NULL) {
741 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
742 goto end;
743 }
744
745 } else {
746 /* reading from a file */
747 in = BIO_new(BIO_s_file());
748 if (in == NULL)
749 goto end;
750
751 if (BIO_read_filename(in, path) <= 0)
752 goto end;
753 }
754
755 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
756 if (!issuer) {
757 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
758 err && *err ? *err : "", path);
759 goto end;
760 }
761 /* no error, fill ckch with new context, old context must be free */
762 if (ckch->ocsp_issuer)
763 X509_free(ckch->ocsp_issuer);
764 ckch->ocsp_issuer = issuer;
765 ret = 0;
766
767end:
768
769 ERR_clear_error();
770 if (in)
771 BIO_free(in);
772
773 return ret;
774}
775
776/******************** ckch_store functions ***********************************
777 * The ckch_store is a structure used to cache and index the SSL files used in
778 * configuration
779 */
780
781/*
782 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
783 */
784void ckch_store_free(struct ckch_store *store)
785{
786 struct ckch_inst *inst, *inst_s;
787
788 if (!store)
789 return;
790
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200791 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200792
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100793 ha_free(&store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200794
795 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
796 ckch_inst_free(inst);
797 }
798 ebmb_delete(&store->node);
799 free(store);
800}
801
802/*
803 * create and initialize a ckch_store
804 * <path> is the key name
805 * <nmemb> is the number of store->ckch objects to allocate
806 *
807 * Return a ckch_store or NULL upon failure.
808 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200809struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200810{
811 struct ckch_store *store;
812 int pathlen;
813
814 pathlen = strlen(filename);
815 store = calloc(1, sizeof(*store) + pathlen + 1);
816 if (!store)
817 return NULL;
818
William Lallemand03c331c2020-05-13 10:10:01 +0200819 memcpy(store->path, filename, pathlen + 1);
820
821 LIST_INIT(&store->ckch_inst);
822 LIST_INIT(&store->crtlist_entry);
823
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200824 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200825 if (!store->ckch)
826 goto error;
827
828 return store;
829error:
830 ckch_store_free(store);
831 return NULL;
832}
833
834/* allocate and duplicate a ckch_store
835 * Return a new ckch_store or NULL */
836struct ckch_store *ckchs_dup(const struct ckch_store *src)
837{
838 struct ckch_store *dst;
839
William Lallemand6c096142021-02-23 14:45:45 +0100840 if (!src)
841 return NULL;
842
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200843 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100844 if (!dst)
845 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200846
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200847 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
848 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200849
850 return dst;
851
852error:
853 ckch_store_free(dst);
854
855 return NULL;
856}
857
858/*
859 * lookup a path into the ckchs tree.
860 */
861struct ckch_store *ckchs_lookup(char *path)
862{
863 struct ebmb_node *eb;
864
865 eb = ebst_lookup(&ckchs_tree, path);
866 if (!eb)
867 return NULL;
868
869 return ebmb_entry(eb, struct ckch_store, node);
870}
871
872/*
873 * This function allocate a ckch_store and populate it with certificates from files.
874 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200875struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200876{
877 struct ckch_store *ckchs;
878
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200879 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200880 if (!ckchs) {
881 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
882 goto end;
883 }
William Lallemand03c331c2020-05-13 10:10:01 +0200884
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200885 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
886 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200887
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200888 /* insert into the ckchs tree */
889 memcpy(ckchs->path, path, strlen(path) + 1);
890 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200891 return ckchs;
892
893end:
894 ckch_store_free(ckchs);
895
896 return NULL;
897}
898
William Lallemandfa1d8b42020-05-13 15:46:10 +0200899
900/******************** ckch_inst functions ******************************/
901
902/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
903/* The caller must use the lock of the bind_conf if used with inserted SNIs */
904void ckch_inst_free(struct ckch_inst *inst)
905{
906 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100907 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200908
909 if (inst == NULL)
910 return;
911
912 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
913 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200914 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200915 ebmb_delete(&sni->name);
916 free(sni);
917 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100918 SSL_CTX_free(inst->ctx);
919 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200920 LIST_DELETE(&inst->by_ckchs);
921 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100922
923 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
924 LIST_DELETE(&link_ref->link->list);
925 LIST_DELETE(&link_ref->list);
926 free(link_ref);
927 }
928
William Lallemandfa1d8b42020-05-13 15:46:10 +0200929 free(inst);
930}
931
932/* Alloc and init a ckch_inst */
933struct ckch_inst *ckch_inst_new()
934{
935 struct ckch_inst *ckch_inst;
936
937 ckch_inst = calloc(1, sizeof *ckch_inst);
938 if (!ckch_inst)
939 return NULL;
940
941 LIST_INIT(&ckch_inst->sni_ctx);
942 LIST_INIT(&ckch_inst->by_ckchs);
943 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100944 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200945
946 return ckch_inst;
947}
948
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200949
950/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100951struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200952
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100953/*
954 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
955 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
956 * during a set cafile/commit cafile cycle there might be two entries for any
957 * given path, the original one and the new one set via the CLI but not
958 * committed yet).
959 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100960struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200961{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100962 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200963 struct ebmb_node *eb;
964
965 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100966 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200967 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100968 /* The ebst_lookup in a tree that has duplicates returns the
969 * oldest entry first. If we want the latest entry, we need to
970 * iterate over all the duplicates until we find the last one
971 * (in our case there should never be more than two entries for
972 * any given path). */
973 if (oldest_entry)
974 return ca_e;
975 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200976 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100977 return ca_e;
978}
979
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +0100980int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
981{
982 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
983}
984
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100985X509_STORE* ssl_store_get0_locations_file(char *path)
986{
987 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
988
989 if (ca_e)
990 return ca_e->ca_store;
991
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200992 return NULL;
993}
994
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +0100995/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +0200996struct cafile_entry *ssl_store_create_cafile_entry(char *path, X509_STORE *store, enum cafile_type type)
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +0100997{
998 struct cafile_entry *ca_e;
999 int pathlen;
1000
1001 pathlen = strlen(path);
1002
1003 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
1004 if (ca_e) {
1005 memcpy(ca_e->path, path, pathlen + 1);
1006 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001007 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001008 LIST_INIT(&ca_e->ckch_inst_link);
1009 }
1010 return ca_e;
1011}
1012
1013/* Delete a cafile_entry. The caller is responsible from removing this entry
1014 * from the cafile_tree first if is was previously added into it. */
1015void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1016{
1017 struct ckch_inst_link *link, *link_s;
1018 if (!ca_e)
1019 return;
1020
1021 X509_STORE_free(ca_e->ca_store);
1022
1023 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1024 struct ckch_inst *inst = link->ckch_inst;
1025 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1026 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1027 if (link_ref->link == link) {
1028 LIST_DELETE(&link_ref->list);
1029 free(link_ref);
1030 break;
1031 }
1032 }
1033 LIST_DELETE(&link->list);
1034 free(link);
1035 }
1036
1037 free(ca_e);
1038}
1039
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001040/*
1041 * Build a cafile_entry out of a buffer instead of out of a file.
1042 * This function is used when the "commit ssl ca-file" cli command is used.
1043 * It can parse CERTIFICATE sections as well as CRL ones.
1044 * Returns 0 in case of success, 1 otherwise.
1045 */
1046int ssl_store_load_ca_from_buf(struct cafile_entry *ca_e, char *cert_buf)
1047{
1048 int retval = 0;
1049
1050 if (!ca_e)
1051 return 1;
1052
1053 if (!ca_e->ca_store) {
1054 ca_e->ca_store = X509_STORE_new();
1055 if (ca_e->ca_store) {
1056 BIO *bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1057 if (bio) {
1058 X509_INFO *info;
1059 int i;
1060 STACK_OF(X509_INFO) *infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1061 if (!infos)
1062 {
1063 BIO_free(bio);
1064 return 1;
1065 }
1066
1067 for (i = 0; i < sk_X509_INFO_num(infos) && !retval; i++) {
1068 info = sk_X509_INFO_value(infos, i);
1069 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1070 if (info->x509) {
1071 retval = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1072 }
1073 if (!retval && info->crl) {
1074 retval = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
1075 }
1076 }
1077 retval = retval || (i != sk_X509_INFO_num(infos));
1078
1079 /* Cleanup */
1080 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1081 BIO_free(bio);
1082 }
1083 }
1084 }
1085
1086 return retval;
1087}
1088
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001089int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001090{
1091 X509_STORE *store = ssl_store_get0_locations_file(path);
1092
1093 /* If this function is called by the CLI, we should not call the
1094 * X509_STORE_load_locations function because it performs forbidden disk
1095 * accesses. */
1096 if (!store && create_if_none) {
1097 struct cafile_entry *ca_e;
1098 store = X509_STORE_new();
1099 if (X509_STORE_load_locations(store, path, NULL)) {
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001100 ca_e = ssl_store_create_cafile_entry(path, store, type);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001101 if (ca_e) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001102 ebst_insert(&cafile_tree, &ca_e->node);
1103 }
1104 } else {
1105 X509_STORE_free(store);
1106 store = NULL;
1107 }
1108 }
1109 return (store != NULL);
1110}
1111
1112
William Lallemandda8584c2020-05-14 10:14:37 +02001113/*************************** CLI commands ***********************/
1114
1115/* Type of SSL payloads that can be updated over the CLI */
1116
1117enum {
1118 CERT_TYPE_PEM = 0,
1119 CERT_TYPE_KEY,
1120#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1121 CERT_TYPE_OCSP,
1122#endif
1123 CERT_TYPE_ISSUER,
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001124#ifdef HAVE_SSL_SCTL
William Lallemandda8584c2020-05-14 10:14:37 +02001125 CERT_TYPE_SCTL,
1126#endif
1127 CERT_TYPE_MAX,
1128};
1129
1130struct {
1131 const char *ext;
1132 int type;
1133 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
1134 /* add a parsing callback */
1135} cert_exts[CERT_TYPE_MAX+1] = {
1136 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
1137 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
1138#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1139 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
1140#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001141#ifdef HAVE_SSL_SCTL
William Lallemandda8584c2020-05-14 10:14:37 +02001142 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
1143#endif
1144 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1145 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
1146};
1147
1148
1149/* release function of the `show ssl cert' command */
1150static void cli_release_show_cert(struct appctx *appctx)
1151{
1152 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1153}
1154
1155/* IO handler of "show ssl cert <filename>" */
1156static int cli_io_handler_show_cert(struct appctx *appctx)
1157{
1158 struct buffer *trash = alloc_trash_chunk();
1159 struct ebmb_node *node;
1160 struct stream_interface *si = appctx->owner;
1161 struct ckch_store *ckchs;
1162
1163 if (trash == NULL)
1164 return 1;
1165
1166 if (!appctx->ctx.ssl.old_ckchs) {
1167 if (ckchs_transaction.old_ckchs) {
1168 ckchs = ckchs_transaction.old_ckchs;
1169 chunk_appendf(trash, "# transaction\n");
William Lallemand5685ccf2020-09-16 16:12:25 +02001170 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001171 }
1172 }
1173
1174 if (!appctx->ctx.cli.p0) {
1175 chunk_appendf(trash, "# filename\n");
1176 node = ebmb_first(&ckchs_tree);
1177 } else {
1178 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
1179 }
1180 while (node) {
1181 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001182 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001183
1184 node = ebmb_next(node);
1185 if (ci_putchk(si_ic(si), trash) == -1) {
1186 si_rx_room_blk(si);
1187 goto yield;
1188 }
1189 }
1190
1191 appctx->ctx.cli.p0 = NULL;
1192 free_trash_chunk(trash);
1193 return 1;
1194yield:
1195
1196 free_trash_chunk(trash);
1197 appctx->ctx.cli.p0 = ckchs;
1198 return 0; /* should come back */
1199}
1200
1201/*
1202 * Extract and format the DNS SAN extensions and copy result into a chuink
1203 * Return 0;
1204 */
1205#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1206static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1207{
1208 int i;
1209 char *str;
1210 STACK_OF(GENERAL_NAME) *names = NULL;
1211
1212 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1213 if (names) {
1214 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1215 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1216 if (i > 0)
1217 chunk_appendf(out, ", ");
1218 if (name->type == GEN_DNS) {
1219 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1220 chunk_appendf(out, "DNS:%s", str);
1221 OPENSSL_free(str);
1222 }
1223 }
1224 }
1225 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1226 }
1227 return 0;
1228}
1229#endif
1230
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001231/*
1232 * Build the ckch_inst_link that will be chained in the CA file entry and the
1233 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1234 * Return 0 in case of success.
1235 */
1236static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1237{
1238 struct ckch_inst_link *new_link;
1239 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1240 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1241 typeof(link), list);
1242 /* Do not add multiple references to the same
1243 * instance in a cafile_entry */
1244 if (link->ckch_inst == ckch_inst) {
1245 return 1;
1246 }
1247 }
1248
1249 new_link = calloc(1, sizeof(*new_link));
1250 if (new_link) {
1251 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1252 if (!new_link_ref) {
1253 free(new_link);
1254 return 1;
1255 }
1256
1257 new_link->ckch_inst = ckch_inst;
1258 new_link_ref->link = new_link;
1259 LIST_INIT(&new_link->list);
1260 LIST_INIT(&new_link_ref->list);
1261
1262 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1263 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1264 }
1265
1266 return 0;
1267}
1268
1269
1270/*
1271 * Link a CA file tree entry to the ckch instance that uses it.
1272 * To determine if and which CA file tree entries need to be linked to the
1273 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1274 * processing the verify option.
1275 * This function works for a frontend as well as for a backend, depending on the
1276 * configuration parameters given (bind_conf or server).
1277 */
1278void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1279 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1280{
1281 int verify = SSL_VERIFY_NONE;
1282
1283 if (srv) {
1284
1285 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1286 verify = SSL_VERIFY_PEER;
1287 switch (srv->ssl_ctx.verify) {
1288 case SSL_SOCK_VERIFY_NONE:
1289 verify = SSL_VERIFY_NONE;
1290 break;
1291 case SSL_SOCK_VERIFY_REQUIRED:
1292 verify = SSL_VERIFY_PEER;
1293 break;
1294 }
1295 }
1296 else {
1297 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1298 case SSL_SOCK_VERIFY_NONE:
1299 verify = SSL_VERIFY_NONE;
1300 break;
1301 case SSL_SOCK_VERIFY_OPTIONAL:
1302 verify = SSL_VERIFY_PEER;
1303 break;
1304 case SSL_SOCK_VERIFY_REQUIRED:
1305 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1306 break;
1307 }
1308 }
1309
1310 if (verify & SSL_VERIFY_PEER) {
1311 struct cafile_entry *ca_file_entry = NULL;
1312 struct cafile_entry *ca_verify_file_entry = NULL;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001313 struct cafile_entry *crl_file_entry = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001314 if (srv) {
1315 if (srv->ssl_ctx.ca_file) {
1316 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1317
1318 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001319 if (srv->ssl_ctx.crl_file) {
1320 crl_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.crl_file, 0);
1321 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001322 }
1323 else {
1324 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1325 char *ca_verify_file = (ssl_conf && ssl_conf->ca_verify_file) ? ssl_conf->ca_verify_file : bind_conf->ssl_conf.ca_verify_file;
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001326 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001327
1328 if (ca_file)
1329 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1330 if (ca_verify_file)
1331 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001332 if (crl_file)
1333 crl_file_entry = ssl_store_get_cafile_entry(crl_file, 0);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001334 }
1335
1336 if (ca_file_entry) {
1337 /* If we have a ckch instance that is not already in the
1338 * cafile_entry's list, add it to it. */
1339 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1340 return;
1341
1342 }
1343 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1344 /* If we have a ckch instance that is not already in the
1345 * cafile_entry's list, add it to it. */
1346 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1347 return;
1348 }
Remi Tricot-Le Bretonf81c70c2021-04-20 16:54:21 +02001349 if (crl_file_entry) {
1350 /* If we have a ckch instance that is not already in the
1351 * cafile_entry's list, add it to it. */
1352 if (do_chain_inst_and_cafile(crl_file_entry, ckch_inst))
1353 return;
1354 }
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001355 }
1356}
1357
William Lallemandda8584c2020-05-14 10:14:37 +02001358
1359
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001360static int show_cert_detail(X509 *cert, STACK_OF(X509) *chain, struct buffer *out)
William Lallemandda8584c2020-05-14 10:14:37 +02001361{
William Lallemandda8584c2020-05-14 10:14:37 +02001362 BIO *bio = NULL;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001363 struct buffer *tmp = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001364 int i;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001365 int write = -1;
1366 unsigned int len = 0;
1367 X509_NAME *name = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001368
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001369 if (!tmp)
1370 return -1;
William Lallemandda8584c2020-05-14 10:14:37 +02001371
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001372 if (!cert)
William Lallemand5685ccf2020-09-16 16:12:25 +02001373 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001374
William Lallemand5685ccf2020-09-16 16:12:25 +02001375 if (chain == NULL) {
1376 struct issuer_chain *issuer;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001377 issuer = ssl_get0_issuer_chain(cert);
William Lallemand5685ccf2020-09-16 16:12:25 +02001378 if (issuer) {
1379 chain = issuer->chain;
1380 chunk_appendf(out, "Chain Filename: ");
1381 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001382 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001383 }
1384 chunk_appendf(out, "Serial: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001385 if (ssl_sock_get_serial(cert, tmp) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001386 goto end;
1387 dump_binary(out, tmp->area, tmp->data);
1388 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001389
William Lallemand5685ccf2020-09-16 16:12:25 +02001390 chunk_appendf(out, "notBefore: ");
1391 chunk_reset(tmp);
1392 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1393 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001394 if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001395 goto end;
1396 write = BIO_read(bio, tmp->area, tmp->size-1);
1397 tmp->area[write] = '\0';
1398 BIO_free(bio);
1399 bio = NULL;
1400 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001401
William Lallemand5685ccf2020-09-16 16:12:25 +02001402 chunk_appendf(out, "notAfter: ");
1403 chunk_reset(tmp);
1404 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1405 goto end;
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001406 if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001407 goto end;
1408 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1409 goto end;
1410 tmp->area[write] = '\0';
1411 BIO_free(bio);
1412 bio = NULL;
1413 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001414
1415#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001416 chunk_appendf(out, "Subject Alternative Name: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001417 if (ssl_sock_get_san_oneline(cert, out) == -1)
William Lallemand5685ccf2020-09-16 16:12:25 +02001418 goto end;
1419 *(out->area + out->data) = '\0';
1420 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001421#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001422 chunk_reset(tmp);
1423 chunk_appendf(out, "Algorithm: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001424 if (cert_get_pkey_algo(cert, tmp) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001425 goto end;
1426 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001427
William Lallemand5685ccf2020-09-16 16:12:25 +02001428 chunk_reset(tmp);
1429 chunk_appendf(out, "SHA1 FingerPrint: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001430 if (X509_digest(cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemand5685ccf2020-09-16 16:12:25 +02001431 goto end;
1432 tmp->data = len;
1433 dump_binary(out, tmp->area, tmp->data);
1434 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001435
William Lallemand5685ccf2020-09-16 16:12:25 +02001436 chunk_appendf(out, "Subject: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001437 if ((name = X509_get_subject_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001438 goto end;
1439 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1440 goto end;
1441 *(tmp->area + tmp->data) = '\0';
1442 chunk_appendf(out, "%s\n", tmp->area);
1443
1444 chunk_appendf(out, "Issuer: ");
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001445 if ((name = X509_get_issuer_name(cert)) == NULL)
William Lallemand5685ccf2020-09-16 16:12:25 +02001446 goto end;
1447 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1448 goto end;
1449 *(tmp->area + tmp->data) = '\0';
1450 chunk_appendf(out, "%s\n", tmp->area);
1451
1452 /* Displays subject of each certificate in the chain */
1453 for (i = 0; i < sk_X509_num(chain); i++) {
1454 X509 *ca = sk_X509_value(chain, i);
1455
1456 chunk_appendf(out, "Chain Subject: ");
1457 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001458 goto end;
1459 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1460 goto end;
1461 *(tmp->area + tmp->data) = '\0';
1462 chunk_appendf(out, "%s\n", tmp->area);
1463
William Lallemand5685ccf2020-09-16 16:12:25 +02001464 chunk_appendf(out, "Chain Issuer: ");
1465 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001466 goto end;
1467 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1468 goto end;
1469 *(tmp->area + tmp->data) = '\0';
1470 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001471 }
1472
1473end:
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001474 if (bio)
1475 BIO_free(bio);
1476 free_trash_chunk(tmp);
1477
1478 return 0;
1479}
1480
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001481#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1482/*
1483 * Build the OCSP tree entry's key for a given ckch_store.
1484 * Returns a negative value in case of error.
1485 */
1486static int ckch_store_build_certid(struct ckch_store *ckch_store, unsigned char certid[128], unsigned int *key_length)
1487{
1488 OCSP_RESPONSE *resp;
1489 OCSP_BASICRESP *bs = NULL;
1490 OCSP_SINGLERESP *sr;
1491 OCSP_CERTID *id;
1492 unsigned char *p = NULL;
1493
1494 if (!key_length)
1495 return -1;
1496
1497 *key_length = 0;
1498
1499 if (!ckch_store->ckch->ocsp_response)
1500 return 0;
1501
1502 p = (unsigned char *) ckch_store->ckch->ocsp_response->area;
1503
1504 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
1505 ckch_store->ckch->ocsp_response->data);
1506 if (!resp) {
1507 goto end;
1508 }
1509
1510 bs = OCSP_response_get1_basic(resp);
1511 if (!bs) {
1512 goto end;
1513 }
1514
1515 sr = OCSP_resp_get0(bs, 0);
1516 if (!sr) {
1517 goto end;
1518 }
1519
1520 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
1521
1522 p = certid;
1523 *key_length = i2d_OCSP_CERTID(id, &p);
1524
1525end:
1526 return *key_length > 0;
1527}
1528#endif
1529
1530/*
1531 * Dump the OCSP certificate key (if it exists) of certificate <ckch> into
1532 * buffer <out>.
1533 * Returns 0 in case of success.
1534 */
1535static int ckch_store_show_ocsp_certid(struct ckch_store *ckch_store, struct buffer *out)
1536{
1537#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1538 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {};
1539 unsigned int key_length = 0;
1540 int i;
1541
1542 if (ckch_store_build_certid(ckch_store, (unsigned char*)key, &key_length) >= 0) {
1543 /* Dump the CERTID info */
1544 chunk_appendf(out, "OCSP Response Key: ");
1545 for (i = 0; i < key_length; ++i) {
1546 chunk_appendf(out, "%02x", key[i]);
1547 }
1548 chunk_appendf(out, "\n");
1549 }
1550#endif
1551
1552 return 0;
1553}
1554
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001555
1556/* IO handler of the details "show ssl cert <filename>" */
1557static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1558{
1559 struct stream_interface *si = appctx->owner;
1560 struct ckch_store *ckchs = appctx->ctx.cli.p0;
1561 struct buffer *out = alloc_trash_chunk();
1562 int retval = 0;
1563
1564 if (!out)
1565 goto end_no_putchk;
1566
1567 chunk_appendf(out, "Filename: ");
1568 if (ckchs == ckchs_transaction.new_ckchs)
1569 chunk_appendf(out, "*");
1570 chunk_appendf(out, "%s\n", ckchs->path);
1571
1572 chunk_appendf(out, "Status: ");
1573 if (ckchs->ckch->cert == NULL)
1574 chunk_appendf(out, "Empty\n");
1575 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1576 chunk_appendf(out, "Unused\n");
1577 else
1578 chunk_appendf(out, "Used\n");
1579
1580 retval = show_cert_detail(ckchs->ckch->cert, ckchs->ckch->chain, out);
1581 if (retval < 0)
1582 goto end_no_putchk;
1583 else if (retval)
1584 goto end;
1585
Remi Tricot-Le Bretonda968f62021-06-10 13:51:14 +02001586 ckch_store_show_ocsp_certid(ckchs, out);
1587
Remi Tricot-Le Breton523f0e42021-03-16 10:11:44 +01001588end:
William Lallemandda8584c2020-05-14 10:14:37 +02001589 if (ci_putchk(si_ic(si), out) == -1) {
1590 si_rx_room_blk(si);
1591 goto yield;
1592 }
1593
1594end_no_putchk:
William Lallemandda8584c2020-05-14 10:14:37 +02001595 free_trash_chunk(out);
1596 return 1;
1597yield:
William Lallemandda8584c2020-05-14 10:14:37 +02001598 free_trash_chunk(out);
1599 return 0; /* should come back */
1600}
1601
1602/* parsing function for 'show ssl cert [certfile]' */
1603static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1604{
1605 struct ckch_store *ckchs;
1606
1607 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1608 return cli_err(appctx, "Can't allocate memory!\n");
1609
1610 /* The operations on the CKCH architecture are locked so we can
1611 * manipulate ckch_store and ckch_inst */
1612 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1613 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1614
1615 /* check if there is a certificate to lookup */
1616 if (*args[3]) {
1617 if (*args[3] == '*') {
1618 if (!ckchs_transaction.new_ckchs)
1619 goto error;
1620
1621 ckchs = ckchs_transaction.new_ckchs;
1622
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001623 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001624 goto error;
1625
1626 } else {
1627 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1628 goto error;
1629
1630 }
1631
William Lallemandda8584c2020-05-14 10:14:37 +02001632 appctx->ctx.cli.p0 = ckchs;
1633 /* use the IO handler that shows details */
1634 appctx->io_handler = cli_io_handler_show_cert_detail;
1635 }
1636
1637 return 0;
1638
1639error:
1640 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1641 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1642}
1643
1644/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1645static void cli_release_commit_cert(struct appctx *appctx)
1646{
1647 struct ckch_store *new_ckchs;
1648
1649 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1650
1651 if (appctx->st2 != SETCERT_ST_FIN) {
1652 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1653 new_ckchs = appctx->ctx.ssl.new_ckchs;
1654
1655 /* if the allocation failed, we need to free everything from the temporary list */
1656 ckch_store_free(new_ckchs);
1657 }
1658}
1659
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001660
1661/*
1662 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1663 * specific ckch_store.
1664 * Returns 0 in case of success, 1 otherwise.
1665 */
1666static int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1667 struct ckch_inst **new_inst, char **err)
1668{
1669 int retval = 0;
1670 int errcode = 0;
1671 struct sni_ctx *sc0, *sc0s;
1672 char **sni_filter = NULL;
1673 int fcount = 0;
1674
1675 if (ckchi->crtlist_entry) {
1676 sni_filter = ckchi->crtlist_entry->filters;
1677 fcount = ckchi->crtlist_entry->fcount;
1678 }
1679
1680 if (ckchi->is_server_instance)
1681 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
1682 else
1683 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
1684
1685 if (errcode & ERR_CODE)
1686 return 1;
1687
1688 /* if the previous ckchi was used as the default */
1689 if (ckchi->is_default)
1690 (*new_inst)->is_default = 1;
1691
1692 (*new_inst)->is_server_instance = ckchi->is_server_instance;
1693 (*new_inst)->server = ckchi->server;
1694 /* Create a new SSL_CTX and link it to the new instance. */
1695 if ((*new_inst)->is_server_instance) {
1696 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
1697 if (retval)
1698 return 1;
1699 }
1700
1701 /* create the link to the crtlist_entry */
1702 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
1703
1704 /* we need to initialize the SSL_CTX generated */
1705 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
1706 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
1707 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
1708 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
1709 if (errcode & ERR_CODE)
1710 return 1;
1711 }
1712 }
1713
1714 return 0;
1715}
1716
1717/*
1718 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
1719 * a server's main ckch instance.
1720 */
1721static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
1722{
1723 /* The bind_conf will be null on server ckch_instances. */
1724 if (ckchi->is_server_instance) {
1725 int i;
1726 /* a lock is needed here since we have to free the SSL cache */
1727 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1728 /* free the server current SSL_CTX */
1729 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
1730 /* Actual ssl context update */
1731 SSL_CTX_up_ref(ckchi->ctx);
1732 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
1733 ckchi->server->ssl_ctx.inst = ckchi;
1734
1735 /* flush the session cache of the server */
1736 for (i = 0; i < global.nbthread; i++) {
1737 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
1738 }
1739 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1740
1741 } else {
1742 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1743 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
1744 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1745 }
1746}
1747
1748/*
1749 * Delete a ckch instance that was replaced after a CLI command.
1750 */
1751static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
1752{
1753 if (ckchi->is_server_instance) {
1754 /* no lock for servers */
1755 ckch_inst_free(ckchi);
1756 } else {
1757 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
1758
1759 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
1760 ckch_inst_free(ckchi);
1761 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
1762 }
1763}
1764
1765
William Lallemandda8584c2020-05-14 10:14:37 +02001766/*
1767 * This function tries to create the new ckch_inst and their SNIs
1768 */
1769static int cli_io_handler_commit_cert(struct appctx *appctx)
1770{
1771 struct stream_interface *si = appctx->owner;
1772 int y = 0;
1773 char *err = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02001774 struct ckch_store *old_ckchs, *new_ckchs = NULL;
1775 struct ckch_inst *ckchi, *ckchis;
1776 struct buffer *trash = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001777 struct crtlist_entry *entry;
1778
1779 if (trash == NULL)
1780 goto error;
1781
1782 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1783 goto error;
1784
1785 while (1) {
1786 switch (appctx->st2) {
1787 case SETCERT_ST_INIT:
1788 /* This state just print the update message */
1789 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
1790 if (ci_putchk(si_ic(si), trash) == -1) {
1791 si_rx_room_blk(si);
1792 goto yield;
1793 }
1794 appctx->st2 = SETCERT_ST_GEN;
1795 /* fallthrough */
1796 case SETCERT_ST_GEN:
1797 /*
1798 * This state generates the ckch instances with their
1799 * sni_ctxs and SSL_CTX.
1800 *
1801 * Since the SSL_CTX generation can be CPU consumer, we
1802 * yield every 10 instances.
1803 */
1804
1805 old_ckchs = appctx->ctx.ssl.old_ckchs;
1806 new_ckchs = appctx->ctx.ssl.new_ckchs;
1807
1808 if (!new_ckchs)
1809 continue;
1810
1811 /* get the next ckchi to regenerate */
1812 ckchi = appctx->ctx.ssl.next_ckchi;
1813 /* we didn't start yet, set it to the first elem */
1814 if (ckchi == NULL)
1815 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
1816
1817 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
1818 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
1819 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02001820
1821 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
1822 if (y >= 10) {
1823 /* save the next ckchi to compute */
1824 appctx->ctx.ssl.next_ckchi = ckchi;
1825 goto yield;
1826 }
1827
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001828 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err))
William Lallemandda8584c2020-05-14 10:14:37 +02001829 goto error;
1830
William Lallemandda8584c2020-05-14 10:14:37 +02001831 /* display one dot per new instance */
1832 chunk_appendf(trash, ".");
1833 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02001834 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02001835 y++;
1836 }
1837 appctx->st2 = SETCERT_ST_INSERT;
1838 /* fallthrough */
1839 case SETCERT_ST_INSERT:
1840 /* The generation is finished, we can insert everything */
1841
1842 old_ckchs = appctx->ctx.ssl.old_ckchs;
1843 new_ckchs = appctx->ctx.ssl.new_ckchs;
1844
1845 if (!new_ckchs)
1846 continue;
1847
1848 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
1849 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
1850 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
1851 ebpt_delete(&entry->node);
1852 /* change the ptr and reinsert the node */
1853 entry->node.key = new_ckchs;
1854 ebpt_insert(&entry->crtlist->entries, &entry->node);
1855 }
1856
William Lallemanda55685b2020-12-15 14:57:46 +01001857 /* insert the new ckch_insts in the crtlist_entry */
1858 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
1859 if (ckchi->crtlist_entry)
Willy Tarreau2b718102021-04-21 07:32:39 +02001860 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
William Lallemanda55685b2020-12-15 14:57:46 +01001861 }
1862
William Lallemandda8584c2020-05-14 10:14:37 +02001863 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
1864 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001865 __ssl_sock_load_new_ckch_instance(ckchi);
William Lallemandda8584c2020-05-14 10:14:37 +02001866 }
1867
1868 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
1869 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001870 __ckch_inst_free_locked(ckchi);
William Lallemandda8584c2020-05-14 10:14:37 +02001871 }
1872
1873 /* Replace the old ckchs by the new one */
1874 ckch_store_free(old_ckchs);
1875 ebst_insert(&ckchs_tree, &new_ckchs->node);
1876 appctx->st2 = SETCERT_ST_FIN;
1877 /* fallthrough */
1878 case SETCERT_ST_FIN:
1879 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001880 ha_free(&ckchs_transaction.path);
William Lallemandda8584c2020-05-14 10:14:37 +02001881 ckchs_transaction.new_ckchs = NULL;
1882 ckchs_transaction.old_ckchs = NULL;
1883 goto end;
1884 }
1885 }
1886end:
1887
1888 chunk_appendf(trash, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001889 chunk_appendf(trash, "Success!\n");
1890 if (ci_putchk(si_ic(si), trash) == -1)
1891 si_rx_room_blk(si);
1892 free_trash_chunk(trash);
1893 /* success: call the release function and don't come back */
1894 return 1;
1895yield:
1896 /* store the state */
1897 if (ci_putchk(si_ic(si), trash) == -1)
1898 si_rx_room_blk(si);
1899 free_trash_chunk(trash);
1900 si_rx_endp_more(si); /* let's come back later */
1901 return 0; /* should come back */
1902
1903error:
1904 /* spin unlock and free are done in the release function */
1905 if (trash) {
1906 chunk_appendf(trash, "\n%sFailed!\n", err);
1907 if (ci_putchk(si_ic(si), trash) == -1)
1908 si_rx_room_blk(si);
1909 free_trash_chunk(trash);
1910 }
1911 /* error: call the release function and don't come back */
1912 return 1;
1913}
1914
1915/*
1916 * Parsing function of 'commit ssl cert'
1917 */
1918static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
1919{
1920 char *err = NULL;
1921
1922 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1923 return 1;
1924
1925 if (!*args[3])
1926 return cli_err(appctx, "'commit ssl cert expects a filename\n");
1927
1928 /* The operations on the CKCH architecture are locked so we can
1929 * manipulate ckch_store and ckch_inst */
1930 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1931 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
1932
1933 if (!ckchs_transaction.path) {
1934 memprintf(&err, "No ongoing transaction! !\n");
1935 goto error;
1936 }
1937
1938 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
1939 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
1940 goto error;
1941 }
1942
William Lallemand5685ccf2020-09-16 16:12:25 +02001943 /* if a certificate is here, a private key must be here too */
1944 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
1945 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
1946 goto error;
1947 }
William Lallemanda9419522020-06-24 16:26:41 +02001948
William Lallemand5685ccf2020-09-16 16:12:25 +02001949 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
1950 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
1951 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02001952 }
1953
1954 /* init the appctx structure */
1955 appctx->st2 = SETCERT_ST_INIT;
1956 appctx->ctx.ssl.next_ckchi = NULL;
1957 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
1958 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
1959
1960 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
1961 return 0;
1962
1963error:
1964
1965 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1966 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
1967
1968 return cli_dynerr(appctx, err);
1969}
1970
1971
1972
1973
1974/*
1975 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
1976 */
1977static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
1978{
1979 struct ckch_store *new_ckchs = NULL;
1980 struct ckch_store *old_ckchs = NULL;
1981 char *err = NULL;
1982 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02001983 int errcode = 0;
1984 char *end;
1985 int type = CERT_TYPE_PEM;
1986 struct cert_key_and_chain *ckch;
1987 struct buffer *buf;
1988
1989 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1990 return 1;
1991
William Lallemandda8584c2020-05-14 10:14:37 +02001992 if (!*args[3] || !payload)
1993 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
1994
1995 /* The operations on the CKCH architecture are locked so we can
1996 * manipulate ckch_store and ckch_inst */
1997 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1998 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
1999
William Lallemand5ba80d62021-05-04 16:17:27 +02002000 if ((buf = alloc_trash_chunk()) == NULL) {
2001 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2002 errcode |= ERR_ALERT | ERR_FATAL;
2003 goto end;
2004 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02002005
William Lallemandda8584c2020-05-14 10:14:37 +02002006 if (!chunk_strcpy(buf, args[3])) {
2007 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2008 errcode |= ERR_ALERT | ERR_FATAL;
2009 goto end;
2010 }
2011
2012 /* check which type of file we want to update */
2013 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
2014 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002015 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02002016 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02002017 buf->data = strlen(buf->area);
William Lallemandda8584c2020-05-14 10:14:37 +02002018 type = cert_exts[i].type;
2019 break;
2020 }
2021 }
2022
2023 appctx->ctx.ssl.old_ckchs = NULL;
2024 appctx->ctx.ssl.new_ckchs = NULL;
2025
2026 /* if there is an ongoing transaction */
2027 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02002028 /* if there is an ongoing transaction, check if this is the same file */
2029 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02002030 /* we didn't find the transaction, must try more cases below */
2031
2032 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
2033 if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
2034 if (!chunk_strcat(buf, ".crt")) {
2035 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2036 errcode |= ERR_ALERT | ERR_FATAL;
2037 goto end;
2038 }
2039
2040 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
2041 /* remove .crt of the error message */
2042 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
2043 b_sub(buf, strlen(".crt"));
2044
2045 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
2046 errcode |= ERR_ALERT | ERR_FATAL;
2047 goto end;
2048 }
2049 }
William Lallemandda8584c2020-05-14 10:14:37 +02002050 }
2051
2052 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
2053
2054 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02002055
William Lallemand95fefa12020-09-09 12:01:33 +02002056 /* lookup for the certificate in the tree */
2057 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02002058
2059 if (!appctx->ctx.ssl.old_ckchs) {
2060 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
2061 if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
2062 if (!chunk_strcat(buf, ".crt")) {
2063 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2064 errcode |= ERR_ALERT | ERR_FATAL;
2065 goto end;
2066 }
2067 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
2068 }
2069 }
William Lallemandda8584c2020-05-14 10:14:37 +02002070 }
2071
2072 if (!appctx->ctx.ssl.old_ckchs) {
2073 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
2074 err ? err : "");
2075 errcode |= ERR_ALERT | ERR_FATAL;
2076 goto end;
2077 }
2078
2079 if (!appctx->ctx.ssl.path) {
2080 /* this is a new transaction, set the path of the transaction */
2081 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
2082 if (!appctx->ctx.ssl.path) {
2083 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2084 errcode |= ERR_ALERT | ERR_FATAL;
2085 goto end;
2086 }
2087 }
2088
2089 old_ckchs = appctx->ctx.ssl.old_ckchs;
2090
2091 /* duplicate the ckch store */
2092 new_ckchs = ckchs_dup(old_ckchs);
2093 if (!new_ckchs) {
2094 memprintf(&err, "%sCannot allocate memory!\n",
2095 err ? err : "");
2096 errcode |= ERR_ALERT | ERR_FATAL;
2097 goto end;
2098 }
2099
William Lallemand95fefa12020-09-09 12:01:33 +02002100 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02002101
2102 /* appply the change on the duplicate */
2103 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
2104 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
2105 errcode |= ERR_ALERT | ERR_FATAL;
2106 goto end;
2107 }
2108
2109 appctx->ctx.ssl.new_ckchs = new_ckchs;
2110
2111 /* we succeed, we can save the ckchs in the transaction */
2112
2113 /* if there wasn't a transaction, update the old ckchs */
2114 if (!ckchs_transaction.old_ckchs) {
2115 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
2116 ckchs_transaction.path = appctx->ctx.ssl.path;
2117 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2118 } else {
2119 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2120
2121 }
2122
2123 /* free the previous ckchs if there was a transaction */
2124 ckch_store_free(ckchs_transaction.new_ckchs);
2125
2126 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
2127
2128
2129 /* creates the SNI ctxs later in the IO handler */
2130
2131end:
2132 free_trash_chunk(buf);
2133
2134 if (errcode & ERR_CODE) {
2135
2136 ckch_store_free(appctx->ctx.ssl.new_ckchs);
2137 appctx->ctx.ssl.new_ckchs = NULL;
2138
2139 appctx->ctx.ssl.old_ckchs = NULL;
2140
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002141 ha_free(&appctx->ctx.ssl.path);
William Lallemandda8584c2020-05-14 10:14:37 +02002142
2143 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2144 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2145 } else {
2146
2147 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2148 return cli_dynmsg(appctx, LOG_NOTICE, err);
2149 }
2150 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2151}
2152
2153/* parsing function of 'abort ssl cert' */
2154static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2155{
2156 char *err = NULL;
2157
2158 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2159 return 1;
2160
2161 if (!*args[3])
2162 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2163
2164 /* The operations on the CKCH architecture are locked so we can
2165 * manipulate ckch_store and ckch_inst */
2166 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2167 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2168
2169 if (!ckchs_transaction.path) {
2170 memprintf(&err, "No ongoing transaction!\n");
2171 goto error;
2172 }
2173
2174 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2175 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2176 goto error;
2177 }
2178
2179 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2180 ckch_store_free(ckchs_transaction.new_ckchs);
2181 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002182 ckchs_transaction.old_ckchs = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002183 ha_free(&ckchs_transaction.path);
William Lallemandda8584c2020-05-14 10:14:37 +02002184
2185 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2186
2187 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2188 return cli_dynmsg(appctx, LOG_NOTICE, err);
2189
2190error:
2191 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2192
2193 return cli_dynerr(appctx, err);
2194}
2195
2196/* parsing function of 'new ssl cert' */
2197static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2198{
2199 struct ckch_store *store;
2200 char *err = NULL;
2201 char *path;
2202
2203 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2204 return 1;
2205
2206 if (!*args[3])
2207 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2208
2209 path = args[3];
2210
2211 /* The operations on the CKCH architecture are locked so we can
2212 * manipulate ckch_store and ckch_inst */
2213 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2214 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2215
2216 store = ckchs_lookup(path);
2217 if (store != NULL) {
2218 memprintf(&err, "Certificate '%s' already exists!\n", path);
2219 store = NULL; /* we don't want to free it */
2220 goto error;
2221 }
2222 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002223 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002224 if (!store) {
2225 memprintf(&err, "unable to allocate memory.\n");
2226 goto error;
2227 }
2228
2229 /* insert into the ckchs tree */
2230 ebst_insert(&ckchs_tree, &store->node);
2231 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2232
2233 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2234 return cli_dynmsg(appctx, LOG_NOTICE, err);
2235error:
2236 free(store);
2237 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2238 return cli_dynerr(appctx, err);
2239}
2240
2241/* parsing function of 'del ssl cert' */
2242static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2243{
2244 struct ckch_store *store;
2245 char *err = NULL;
2246 char *filename;
2247
2248 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2249 return 1;
2250
2251 if (!*args[3])
2252 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2253
2254 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2255 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2256
2257 filename = args[3];
2258
2259 store = ckchs_lookup(filename);
2260 if (store == NULL) {
2261 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2262 goto error;
2263 }
2264 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2265 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2266 goto error;
2267 }
2268
2269 ebmb_delete(&store->node);
2270 ckch_store_free(store);
2271
2272 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2273
2274 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2275 return cli_dynmsg(appctx, LOG_NOTICE, err);
2276
2277error:
2278 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2279 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2280 return cli_dynerr(appctx, err);
2281}
2282
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002283
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01002284
2285/* parsing function of 'new ssl ca-file' */
2286static int cli_parse_new_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2287{
2288 struct cafile_entry *cafile_entry;
2289 char *err = NULL;
2290 char *path;
2291
2292 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2293 return 1;
2294
2295 if (!*args[3])
2296 return cli_err(appctx, "'new ssl ca-file' expects a filename\n");
2297
2298 path = args[3];
2299
2300 /* The operations on the CKCH architecture are locked so we can
2301 * manipulate ckch_store and ckch_inst */
2302 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2303 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
2304
2305 cafile_entry = ssl_store_get_cafile_entry(path, 0);
2306 if (cafile_entry) {
2307 memprintf(&err, "CA file '%s' already exists!\n", path);
2308 goto error;
2309 }
2310
2311 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CERT);
2312 if (!cafile_entry) {
2313 memprintf(&err, "%sCannot allocate memory!\n",
2314 err ? err : "");
2315 goto error;
2316 }
2317
2318 /* Add the newly created cafile_entry to the tree so that
2319 * any new ckch instance created from now can use it. */
2320 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
2321 goto error;
2322
2323 memprintf(&err, "New CA file created '%s'!\n", path);
2324
2325 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2326 return cli_dynmsg(appctx, LOG_NOTICE, err);
2327error:
2328 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2329 return cli_dynerr(appctx, err);
2330}
2331
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002332/*
2333 * Parsing function of `set ssl ca-file`
2334 */
2335static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2336{
2337 char *err = NULL;
2338 int errcode = 0;
2339 struct buffer *buf;
2340
2341 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2342 return 1;
2343
2344 if (!*args[3] || !payload)
2345 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs 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 CA file!\nOperations on certificates are currently locked!\n");
2351
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 }
2357
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 appctx->ctx.ssl.old_cafile_entry = NULL;
2365 appctx->ctx.ssl.new_cafile_entry = NULL;
2366
2367 /* if there is an ongoing transaction */
2368 if (cafile_transaction.path) {
2369 /* if there is an ongoing transaction, check if this is the same file */
2370 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2371 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2372 errcode |= ERR_ALERT | ERR_FATAL;
2373 goto end;
2374 }
2375 appctx->ctx.ssl.old_cafile_entry = cafile_transaction.old_cafile_entry;
2376 }
2377 else {
2378 /* lookup for the certificate in the tree */
2379 appctx->ctx.ssl.old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
2380 }
2381
2382 if (!appctx->ctx.ssl.old_cafile_entry) {
2383 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2384 err ? err : "");
2385 errcode |= ERR_ALERT | ERR_FATAL;
2386 goto end;
2387 }
2388
2389 if (!appctx->ctx.ssl.path) {
2390 /* this is a new transaction, set the path of the transaction */
2391 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_cafile_entry->path);
2392 if (!appctx->ctx.ssl.path) {
2393 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2394 errcode |= ERR_ALERT | ERR_FATAL;
2395 goto end;
2396 }
2397 }
2398
2399 if (appctx->ctx.ssl.new_cafile_entry)
2400 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_cafile_entry);
2401
2402 /* Create a new cafile_entry without adding it to the cafile tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02002403 appctx->ctx.ssl.new_cafile_entry = ssl_store_create_cafile_entry(appctx->ctx.ssl.path, NULL, CAFILE_CERT);
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002404 if (!appctx->ctx.ssl.new_cafile_entry) {
2405 memprintf(&err, "%sCannot allocate memory!\n",
2406 err ? err : "");
2407 errcode |= ERR_ALERT | ERR_FATAL;
2408 goto end;
2409 }
2410
2411 /* Fill the new entry with the new CAs. */
2412 if (ssl_store_load_ca_from_buf(appctx->ctx.ssl.new_cafile_entry, payload)) {
2413 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2414 errcode |= ERR_ALERT | ERR_FATAL;
2415 goto end;
2416 }
2417
2418 /* we succeed, we can save the ca in the transaction */
2419
2420 /* if there wasn't a transaction, update the old CA */
2421 if (!cafile_transaction.old_cafile_entry) {
2422 cafile_transaction.old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2423 cafile_transaction.path = appctx->ctx.ssl.path;
2424 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2425 } else {
2426 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2427 }
2428
2429 /* free the previous CA if there was a transaction */
2430 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2431
2432 cafile_transaction.new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2433
2434 /* creates the SNI ctxs later in the IO handler */
2435
2436end:
2437 free_trash_chunk(buf);
2438
2439 if (errcode & ERR_CODE) {
2440 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_cafile_entry);
2441 appctx->ctx.ssl.new_cafile_entry = NULL;
2442 appctx->ctx.ssl.old_cafile_entry = NULL;
2443
2444 free(appctx->ctx.ssl.path);
2445 appctx->ctx.ssl.path = NULL;
2446
2447 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2448 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2449 } else {
2450
2451 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2452 return cli_dynmsg(appctx, LOG_NOTICE, err);
2453 }
2454}
2455
2456
2457/*
2458 * Parsing function of 'commit ssl ca-file'
2459 */
2460static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2461{
2462 char *err = NULL;
2463
2464 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2465 return 1;
2466
2467 if (!*args[3])
2468 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2469
2470 /* The operations on the CKCH architecture are locked so we can
2471 * manipulate ckch_store and ckch_inst */
2472 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2473 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2474
2475 if (!cafile_transaction.path) {
2476 memprintf(&err, "No ongoing transaction! !\n");
2477 goto error;
2478 }
2479
2480 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2481 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2482 goto error;
2483 }
2484 /* init the appctx structure */
2485 appctx->st2 = SETCERT_ST_INIT;
2486 appctx->ctx.ssl.next_ckchi_link = NULL;
2487 appctx->ctx.ssl.old_cafile_entry = cafile_transaction.old_cafile_entry;
2488 appctx->ctx.ssl.new_cafile_entry = cafile_transaction.new_cafile_entry;
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002489 appctx->ctx.ssl.cafile_type = CAFILE_CERT;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002490
2491 return 0;
2492
2493error:
2494
2495 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2496 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2497
2498 return cli_dynerr(appctx, err);
2499}
2500
2501enum {
2502 CREATE_NEW_INST_OK = 0,
2503 CREATE_NEW_INST_YIELD = -1,
2504 CREATE_NEW_INST_ERR = -2
2505};
2506
2507static inline int __create_new_instance(struct appctx *appctx, struct ckch_inst *ckchi, int *count,
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02002508 struct buffer *trash, char **err)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002509{
2510 struct ckch_inst *new_inst;
2511
2512 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2513 if (*count >= 10) {
2514 /* save the next ckchi to compute */
2515 appctx->ctx.ssl.next_ckchi = ckchi;
2516 return CREATE_NEW_INST_YIELD;
2517 }
2518
2519 /* Rebuild a new ckch instance that uses the same ckch_store
2520 * than a reference ckchi instance but will use a new CA file. */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02002521 if (ckch_inst_rebuild(ckchi->ckch_store, ckchi, &new_inst, err))
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002522 return CREATE_NEW_INST_ERR;
2523
2524 /* display one dot per new instance */
2525 chunk_appendf(trash, ".");
2526 ++(*count);
2527
2528 return CREATE_NEW_INST_OK;
2529}
2530
2531/*
2532 * This function tries to create new ckch instances and their SNIs using a newly
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002533 * set certificate authority (CA file) or a newly set Certificate Revocation
2534 * List (CRL), depending on the command being called.
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002535 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002536static int cli_io_handler_commit_cafile_crlfile(struct appctx *appctx)
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002537{
2538 struct stream_interface *si = appctx->owner;
2539 int y = 0;
2540 char *err = NULL;
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02002541 struct cafile_entry *old_cafile_entry = NULL, *new_cafile_entry = NULL;
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002542 struct ckch_inst_link *ckchi_link;
2543 struct buffer *trash = alloc_trash_chunk();
2544
2545 if (trash == NULL)
2546 goto error;
2547
2548 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2549 goto error;
2550
2551 while (1) {
2552 switch (appctx->st2) {
2553 case SETCERT_ST_INIT:
2554 /* This state just print the update message */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002555 switch (appctx->ctx.ssl.cafile_type) {
2556 case CAFILE_CERT:
2557 chunk_printf(trash, "Committing %s", cafile_transaction.path);
2558 break;
2559 case CAFILE_CRL:
2560 chunk_printf(trash, "Committing %s", crlfile_transaction.path);
2561 break;
2562 default:
2563 goto error;
2564 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002565 if (ci_putchk(si_ic(si), trash) == -1) {
2566 si_rx_room_blk(si);
2567 goto yield;
2568 }
2569 appctx->st2 = SETCERT_ST_GEN;
2570 /* fallthrough */
2571 case SETCERT_ST_GEN:
2572 /*
2573 * This state generates the ckch instances with their
2574 * sni_ctxs and SSL_CTX.
2575 *
2576 * Since the SSL_CTX generation can be CPU consumer, we
2577 * yield every 10 instances.
2578 */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002579 switch (appctx->ctx.ssl.cafile_type) {
2580 case CAFILE_CERT:
2581 old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2582 new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2583 break;
2584 case CAFILE_CRL:
2585 old_cafile_entry = appctx->ctx.ssl.old_crlfile_entry;
2586 new_cafile_entry = appctx->ctx.ssl.new_crlfile_entry;
2587 break;
2588 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002589 if (!new_cafile_entry)
2590 continue;
2591
2592 /* get the next ckchi to regenerate */
2593 ckchi_link = appctx->ctx.ssl.next_ckchi_link;
2594 /* we didn't start yet, set it to the first elem */
2595 if (ckchi_link == NULL) {
2596 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2597 /* Add the newly created cafile_entry to the tree so that
2598 * any new ckch instance created from now can use it. */
2599 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry))
2600 goto error;
2601 }
2602
2603 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02002604 switch (__create_new_instance(appctx, ckchi_link->ckch_inst, &y, trash, &err)) {
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002605 case CREATE_NEW_INST_YIELD:
2606 appctx->ctx.ssl.next_ckchi_link = ckchi_link;
2607 goto yield;
2608 case CREATE_NEW_INST_ERR:
2609 goto error;
2610 default: break;
2611 }
2612 }
2613
2614 appctx->st2 = SETCERT_ST_INSERT;
2615 /* fallthrough */
2616 case SETCERT_ST_INSERT:
2617 /* The generation is finished, we can insert everything */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002618 switch (appctx->ctx.ssl.cafile_type) {
2619 case CAFILE_CERT:
2620 old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2621 new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2622 break;
2623 case CAFILE_CRL:
2624 old_cafile_entry = appctx->ctx.ssl.old_crlfile_entry;
2625 new_cafile_entry = appctx->ctx.ssl.new_crlfile_entry;
2626 break;
2627 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002628 if (!new_cafile_entry)
2629 continue;
2630
2631 /* insert the new ckch_insts in the crtlist_entry */
2632 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2633 if (ckchi_link->ckch_inst->crtlist_entry)
2634 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2635 &ckchi_link->ckch_inst->by_crtlist_entry);
2636 }
2637
2638 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2639 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2640 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2641 }
2642
2643 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2644 list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
2645 __ckch_inst_free_locked(ckchi_link->ckch_inst);
2646 }
2647
2648
2649 /* Remove the old cafile entry from the tree */
2650 ebmb_delete(&old_cafile_entry->node);
2651 ssl_store_delete_cafile_entry(old_cafile_entry);
2652
2653 appctx->st2 = SETCERT_ST_FIN;
2654 /* fallthrough */
2655 case SETCERT_ST_FIN:
2656 /* we achieved the transaction, we can set everything to NULL */
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02002657 switch (appctx->ctx.ssl.cafile_type) {
2658 case CAFILE_CERT:
2659 ha_free(&cafile_transaction.path);
2660 cafile_transaction.old_cafile_entry = NULL;
2661 cafile_transaction.new_cafile_entry = NULL;
2662 break;
2663 case CAFILE_CRL:
2664 ha_free(&crlfile_transaction.path);
2665 crlfile_transaction.old_crlfile_entry = NULL;
2666 crlfile_transaction.new_crlfile_entry = NULL;
2667 break;
2668 }
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002669 goto end;
2670 }
2671 }
2672end:
2673
2674 chunk_appendf(trash, "\n");
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002675 chunk_appendf(trash, "Success!\n");
2676 if (ci_putchk(si_ic(si), trash) == -1)
2677 si_rx_room_blk(si);
2678 free_trash_chunk(trash);
2679 /* success: call the release function and don't come back */
2680 return 1;
2681yield:
2682 /* store the state */
2683 if (ci_putchk(si_ic(si), trash) == -1)
2684 si_rx_room_blk(si);
2685 free_trash_chunk(trash);
2686 si_rx_endp_more(si); /* let's come back later */
2687 return 0; /* should come back */
2688
2689error:
2690 /* spin unlock and free are done in the release function */
2691 if (trash) {
2692 chunk_appendf(trash, "\n%sFailed!\n", err);
2693 if (ci_putchk(si_ic(si), trash) == -1)
2694 si_rx_room_blk(si);
2695 free_trash_chunk(trash);
2696 }
2697 /* error: call the release function and don't come back */
2698 return 1;
2699}
2700
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002701
2702/* parsing function of 'abort ssl ca-file' */
2703static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2704{
2705 char *err = NULL;
2706
2707 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2708 return 1;
2709
2710 if (!*args[3])
2711 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2712
2713 /* The operations on the CKCH architecture are locked so we can
2714 * manipulate ckch_store and ckch_inst */
2715 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2716 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2717
2718 if (!cafile_transaction.path) {
2719 memprintf(&err, "No ongoing transaction!\n");
2720 goto error;
2721 }
2722
2723 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2724 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2725 goto error;
2726 }
2727
2728 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
2729 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2730 cafile_transaction.new_cafile_entry = NULL;
2731 cafile_transaction.old_cafile_entry = NULL;
2732 ha_free(&cafile_transaction.path);
2733
2734 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2735
2736 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2737 return cli_dynmsg(appctx, LOG_NOTICE, err);
2738
2739error:
2740 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2741
2742 return cli_dynerr(appctx, err);
2743}
2744
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002745/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock */
2746static void cli_release_commit_cafile(struct appctx *appctx)
2747{
2748 if (appctx->st2 != SETCERT_ST_FIN) {
2749 struct cafile_entry *new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2750
2751 /* Remove the uncommitted cafile_entry from the tree. */
2752 ebmb_delete(&new_cafile_entry->node);
2753 ssl_store_delete_cafile_entry(new_cafile_entry);
2754 }
2755 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2756}
2757
2758
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01002759/* IO handler of details "show ssl ca-file <filename[:index]>" */
2760static int cli_io_handler_show_cafile_detail(struct appctx *appctx)
2761{
2762 struct stream_interface *si = appctx->owner;
2763 struct cafile_entry *cafile_entry = appctx->ctx.cli.p0;
2764 struct buffer *out = alloc_trash_chunk();
2765 int i;
2766 X509 *cert;
2767 STACK_OF(X509_OBJECT) *objs;
2768 int retval = 0;
2769 long ca_index = (long)appctx->ctx.cli.p1;
2770
2771 if (!out)
2772 goto end_no_putchk;
2773
2774 chunk_appendf(out, "Filename: ");
2775 if (cafile_entry == cafile_transaction.new_cafile_entry)
2776 chunk_appendf(out, "*");
2777 chunk_appendf(out, "%s\n", cafile_entry->path);
2778
2779 chunk_appendf(out, "Status: ");
2780 if (!cafile_entry->ca_store)
2781 chunk_appendf(out, "Empty\n");
2782 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
2783 chunk_appendf(out, "Unused\n");
2784 else
2785 chunk_appendf(out, "Used\n");
2786
2787 if (!cafile_entry->ca_store)
2788 goto end;
2789
2790 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
2791 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
2792 cert = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
2793 if (!cert)
2794 continue;
2795
2796 /* Certificate indexes start at 1 on the CLI output. */
2797 if (ca_index && ca_index-1 != i)
2798 continue;
2799
2800 chunk_appendf(out, "\nCertificate #%d:\n", i+1);
2801 retval = show_cert_detail(cert, NULL, out);
2802 if (retval < 0)
2803 goto end_no_putchk;
2804 else if (retval || ca_index)
2805 goto end;
2806 }
2807
2808end:
2809 if (ci_putchk(si_ic(si), out) == -1) {
2810 si_rx_room_blk(si);
2811 goto yield;
2812 }
2813
2814end_no_putchk:
2815 free_trash_chunk(out);
2816 return 1;
2817yield:
2818 free_trash_chunk(out);
2819 return 0; /* should come back */
2820}
2821
2822
2823/* parsing function for 'show ssl ca-file [cafile[:index]]' */
2824static int cli_parse_show_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2825{
2826 struct cafile_entry *cafile_entry;
2827 long ca_index = 0;
2828 char *colons;
2829 char *err = NULL;
2830
2831 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2832 return cli_err(appctx, "Can't allocate memory!\n");
2833
2834 /* The operations on the CKCH architecture are locked so we can
2835 * manipulate ckch_store and ckch_inst */
2836 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2837 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
2838
2839 /* check if there is a certificate to lookup */
2840 if (*args[3]) {
2841
2842 /* Look for an optional CA index after the CA file name */
2843 colons = strchr(args[3], ':');
2844 if (colons) {
2845 char *endptr;
2846
2847 ca_index = strtol(colons + 1, &endptr, 10);
2848 /* Indexes start at 1 */
2849 if (colons + 1 == endptr || *endptr != '\0' || ca_index <= 0) {
2850 memprintf(&err, "wrong CA index after colons in '%s'!", args[3]);
2851 goto error;
2852 }
2853 *colons = '\0';
2854 }
2855
2856 if (*args[3] == '*') {
2857 if (!cafile_transaction.new_cafile_entry)
2858 goto error;
2859
2860 cafile_entry = cafile_transaction.new_cafile_entry;
2861
2862 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
2863 goto error;
2864
2865 } else {
2866 /* Get the "original" cafile_entry and not the
2867 * uncommitted one if it exists. */
2868 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CERT)
2869 goto error;
2870 }
2871
2872 appctx->ctx.cli.p0 = cafile_entry;
2873 appctx->ctx.cli.p1 = (void*)ca_index;
2874 /* use the IO handler that shows details */
2875 appctx->io_handler = cli_io_handler_show_cafile_detail;
2876 }
2877
2878 return 0;
2879
2880error:
2881 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2882 if (err)
2883 return cli_dynerr(appctx, err);
2884 return cli_err(appctx, "Can't display the CA file : Not found!\n");
2885}
2886
2887
2888/* release function of the 'show ssl ca-file' command */
2889static void cli_release_show_cafile(struct appctx *appctx)
2890{
2891 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2892}
2893
2894
2895/* This function returns the number of certificates in a cafile_entry. */
2896static int get_certificate_count(struct cafile_entry *cafile_entry)
2897{
2898 int cert_count = 0;
2899 STACK_OF(X509_OBJECT) *objs;
2900
2901 if (cafile_entry && cafile_entry->ca_store) {
2902 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
2903 if (objs)
2904 cert_count = sk_X509_OBJECT_num(objs);
2905 }
2906 return cert_count;
2907}
2908
2909/* IO handler of "show ssl ca-file". The command taking a specific CA file name
2910 * is managed in cli_io_handler_show_cafile_detail. */
2911static int cli_io_handler_show_cafile(struct appctx *appctx)
2912{
2913 struct buffer *trash = alloc_trash_chunk();
2914 struct ebmb_node *node;
2915 struct stream_interface *si = appctx->owner;
2916 struct cafile_entry *cafile_entry;
2917
2918 if (trash == NULL)
2919 return 1;
2920
2921 if (!appctx->ctx.ssl.old_cafile_entry) {
2922 if (cafile_transaction.old_cafile_entry) {
2923 chunk_appendf(trash, "# transaction\n");
2924 chunk_appendf(trash, "*%s", cafile_transaction.old_cafile_entry->path);
2925
2926 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_transaction.new_cafile_entry));
2927 }
2928 }
2929
2930 /* First time in this io_handler. */
2931 if (!appctx->ctx.cli.p0) {
2932 chunk_appendf(trash, "# filename\n");
2933 node = ebmb_first(&cafile_tree);
2934 } else {
2935 /* We yielded during a previous call. */
2936 node = &((struct cafile_entry*)appctx->ctx.cli.p0)->node;
2937 }
2938
2939 while (node) {
2940 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
2941 if (cafile_entry->type == CAFILE_CERT) {
2942 chunk_appendf(trash, "%s", cafile_entry->path);
2943
2944 chunk_appendf(trash, " - %d certificate(s)\n", get_certificate_count(cafile_entry));
2945 }
2946
2947 node = ebmb_next(node);
2948 if (ci_putchk(si_ic(si), trash) == -1) {
2949 si_rx_room_blk(si);
2950 goto yield;
2951 }
2952 }
2953
2954 appctx->ctx.cli.p0 = NULL;
2955 free_trash_chunk(trash);
2956 return 1;
2957yield:
2958
2959 free_trash_chunk(trash);
2960 appctx->ctx.cli.p0 = cafile_entry;
2961 return 0; /* should come back */
2962}
2963
Remi Tricot-Le Bretonc3a84772021-03-25 18:13:57 +01002964/* parsing function of 'del ssl ca-file' */
2965static int cli_parse_del_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2966{
2967 struct cafile_entry *cafile_entry;
2968 char *err = NULL;
2969 char *filename;
2970
2971 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2972 return 1;
2973
2974 if (!*args[3])
2975 return cli_err(appctx, "'del ssl ca-file' expects a CA file name\n");
2976
2977 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2978 return cli_err(appctx, "Can't delete the CA file!\nOperations on certificates are currently locked!\n");
2979
2980 filename = args[3];
2981
2982 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
2983 if (!cafile_entry) {
2984 memprintf(&err, "CA file '%s' doesn't exist!\n", filename);
2985 goto error;
2986 }
2987
2988 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
2989 memprintf(&err, "CA file '%s' in use, can't be deleted!\n", filename);
2990 goto error;
2991 }
2992
2993 /* Remove the cafile_entry from the tree */
2994 ebmb_delete(&cafile_entry->node);
2995 ssl_store_delete_cafile_entry(cafile_entry);
2996
2997 memprintf(&err, "CA file '%s' deleted!\n", filename);
2998
2999 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3000 return cli_dynmsg(appctx, LOG_NOTICE, err);
3001
3002error:
3003 memprintf(&err, "Can't remove the CA file: %s\n", err ? err : "");
3004 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3005 return cli_dynerr(appctx, err);
3006}
3007
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003008/* parsing function of 'new ssl crl-file' */
3009static int cli_parse_new_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3010{
3011 struct cafile_entry *cafile_entry;
3012 char *err = NULL;
3013 char *path;
3014
3015 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3016 return 1;
3017
3018 if (!*args[3])
3019 return cli_err(appctx, "'new ssl crl-file' expects a filename\n");
3020
3021 path = args[3];
3022
3023 /* The operations on the CKCH architecture are locked so we can
3024 * manipulate ckch_store and ckch_inst */
3025 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3026 return cli_err(appctx, "Can't create a CA file!\nOperations on certificates are currently locked!\n");
3027
3028 cafile_entry = ssl_store_get_cafile_entry(path, 0);
3029 if (cafile_entry) {
3030 memprintf(&err, "CRL file '%s' already exists!\n", path);
3031 goto error;
3032 }
3033
3034 cafile_entry = ssl_store_create_cafile_entry(path, NULL, CAFILE_CRL);
3035 if (!cafile_entry) {
3036 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3037 goto error;
3038 }
3039
3040 /* Add the newly created cafile_entry to the tree so that
3041 * any new ckch instance created from now can use it. */
3042 if (ssl_store_add_uncommitted_cafile_entry(cafile_entry))
3043 goto error;
3044
3045 memprintf(&err, "New CRL file created '%s'!\n", path);
3046
3047 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3048 return cli_dynmsg(appctx, LOG_NOTICE, err);
3049error:
3050 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3051 return cli_dynerr(appctx, err);
3052}
3053
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003054/* Parsing function of `set ssl crl-file` */
3055static int cli_parse_set_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3056{
3057 char *err = NULL;
3058 int errcode = 0;
3059 struct buffer *buf;
3060
3061 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3062 return 1;
3063
3064 if (!*args[3] || !payload)
3065 return cli_err(appctx, "'set ssl crl-file expects a filename and CAs as a payload\n");
3066
3067 /* The operations on the CKCH architecture are locked so we can
3068 * manipulate ckch_store and ckch_inst */
3069 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3070 return cli_err(appctx, "Can't update the CRL file!\nOperations on certificates are currently locked!\n");
3071
3072 if ((buf = alloc_trash_chunk()) == NULL) {
3073 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3074 errcode |= ERR_ALERT | ERR_FATAL;
3075 goto end;
3076 }
3077
3078 if (!chunk_strcpy(buf, args[3])) {
3079 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3080 errcode |= ERR_ALERT | ERR_FATAL;
3081 goto end;
3082 }
3083
3084 appctx->ctx.ssl.old_crlfile_entry = NULL;
3085 appctx->ctx.ssl.new_crlfile_entry = NULL;
3086
3087 /* if there is an ongoing transaction */
3088 if (crlfile_transaction.path) {
3089 /* if there is an ongoing transaction, check if this is the same file */
3090 if (strcmp(crlfile_transaction.path, buf->area) != 0) {
3091 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, buf->area);
3092 errcode |= ERR_ALERT | ERR_FATAL;
3093 goto end;
3094 }
3095 appctx->ctx.ssl.old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
3096 }
3097 else {
3098 /* lookup for the certificate in the tree */
3099 appctx->ctx.ssl.old_crlfile_entry = ssl_store_get_cafile_entry(buf->area, 0);
3100 }
3101
3102 if (!appctx->ctx.ssl.old_crlfile_entry) {
3103 memprintf(&err, "%sCan't replace a CRL file which is not referenced by the configuration!\n",
3104 err ? err : "");
3105 errcode |= ERR_ALERT | ERR_FATAL;
3106 goto end;
3107 }
3108
3109 if (!appctx->ctx.ssl.path) {
3110 /* this is a new transaction, set the path of the transaction */
3111 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_crlfile_entry->path);
3112 if (!appctx->ctx.ssl.path) {
3113 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
3114 errcode |= ERR_ALERT | ERR_FATAL;
3115 goto end;
3116 }
3117 }
3118
3119 if (appctx->ctx.ssl.new_crlfile_entry)
3120 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_crlfile_entry);
3121
3122 /* Create a new cafile_entry without adding it to the cafile tree. */
3123 appctx->ctx.ssl.new_crlfile_entry = ssl_store_create_cafile_entry(appctx->ctx.ssl.path, NULL, CAFILE_CRL);
3124 if (!appctx->ctx.ssl.new_crlfile_entry) {
3125 memprintf(&err, "%sCannot allocate memory!\n", err ? err : "");
3126 errcode |= ERR_ALERT | ERR_FATAL;
3127 goto end;
3128 }
3129
3130 /* Fill the new entry with the new CRL. */
3131 if (ssl_store_load_ca_from_buf(appctx->ctx.ssl.new_crlfile_entry, payload)) {
3132 memprintf(&err, "%sInvalid payload\n", err ? err : "");
3133 errcode |= ERR_ALERT | ERR_FATAL;
3134 goto end;
3135 }
3136
3137 /* we succeed, we can save the crl in the transaction */
3138
3139 /* if there wasn't a transaction, update the old CA */
3140 if (!crlfile_transaction.old_crlfile_entry) {
3141 crlfile_transaction.old_crlfile_entry = appctx->ctx.ssl.old_crlfile_entry;
3142 crlfile_transaction.path = appctx->ctx.ssl.path;
3143 err = memprintf(&err, "transaction created for CA %s!\n", crlfile_transaction.path);
3144 } else {
3145 err = memprintf(&err, "transaction updated for CA %s!\n", crlfile_transaction.path);
3146 }
3147
3148 /* free the previous CRL file if there was a transaction */
3149 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3150
3151 crlfile_transaction.new_crlfile_entry = appctx->ctx.ssl.new_crlfile_entry;
3152
3153 /* creates the SNI ctxs later in the IO handler */
3154
3155end:
3156 free_trash_chunk(buf);
3157
3158 if (errcode & ERR_CODE) {
3159 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_crlfile_entry);
3160 appctx->ctx.ssl.new_crlfile_entry = NULL;
3161 appctx->ctx.ssl.old_crlfile_entry = NULL;
3162
3163 free(appctx->ctx.ssl.path);
3164 appctx->ctx.ssl.path = NULL;
3165
3166 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3167 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
3168 } else {
3169
3170 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3171 return cli_dynmsg(appctx, LOG_NOTICE, err);
3172 }
3173}
3174
3175/* Parsing function of 'commit ssl crl-file' */
3176static int cli_parse_commit_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3177{
3178 char *err = NULL;
3179
3180 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3181 return 1;
3182
3183 if (!*args[3])
3184 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
3185
3186 /* The operations on the CKCH architecture are locked so we can
3187 * manipulate ckch_store and ckch_inst */
3188 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3189 return cli_err(appctx, "Can't commit the CRL file!\nOperations on certificates are currently locked!\n");
3190
3191 if (!crlfile_transaction.path) {
3192 memprintf(&err, "No ongoing transaction! !\n");
3193 goto error;
3194 }
3195
3196 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3197 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", crlfile_transaction.path, args[3]);
3198 goto error;
3199 }
3200 /* init the appctx structure */
3201 appctx->st2 = SETCERT_ST_INIT;
3202 appctx->ctx.ssl.next_ckchi = NULL;
3203 appctx->ctx.ssl.old_crlfile_entry = crlfile_transaction.old_crlfile_entry;
3204 appctx->ctx.ssl.new_crlfile_entry = crlfile_transaction.new_crlfile_entry;
3205 appctx->ctx.ssl.cafile_type = CAFILE_CRL;
3206
3207 return 0;
3208
3209error:
3210
3211 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3212 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
3213
3214 return cli_dynerr(appctx, err);
3215}
3216
3217
3218/* release function of the `commit ssl crl-file' command, free things and unlock the spinlock */
3219static void cli_release_commit_crlfile(struct appctx *appctx)
3220{
3221 if (appctx->st2 != SETCERT_ST_FIN) {
3222 struct cafile_entry *new_crlfile_entry = appctx->ctx.ssl.new_crlfile_entry;
3223
3224 /* Remove the uncommitted cafile_entry from the tree. */
3225 ebmb_delete(&new_crlfile_entry->node);
3226 ssl_store_delete_cafile_entry(new_crlfile_entry);
3227 }
3228 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3229}
3230
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003231/* parsing function of 'del ssl crl-file' */
3232static int cli_parse_del_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3233{
3234 struct cafile_entry *cafile_entry;
3235 char *err = NULL;
3236 char *filename;
3237
3238 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3239 return 1;
3240
3241 if (!*args[3])
3242 return cli_err(appctx, "'del ssl crl-file' expects a CRL file name\n");
3243
3244 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3245 return cli_err(appctx, "Can't delete the CRL file!\nOperations on certificates are currently locked!\n");
3246
3247 filename = args[3];
3248
3249 cafile_entry = ssl_store_get_cafile_entry(filename, 0);
3250 if (!cafile_entry) {
3251 memprintf(&err, "CRL file '%s' doesn't exist!\n", filename);
3252 goto error;
3253 }
3254 if (cafile_entry->type != CAFILE_CRL) {
3255 memprintf(&err, "'del ssl crl-file' does not work on CA files!\n");
3256 goto error;
3257 }
3258
3259 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
3260 memprintf(&err, "CRL file '%s' in use, can't be deleted!\n", filename);
3261 goto error;
3262 }
3263
3264 /* Remove the cafile_entry from the tree */
3265 ebmb_delete(&cafile_entry->node);
3266 ssl_store_delete_cafile_entry(cafile_entry);
3267
3268 memprintf(&err, "CRL file '%s' deleted!\n", filename);
3269
3270 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3271 return cli_dynmsg(appctx, LOG_NOTICE, err);
3272
3273error:
3274 memprintf(&err, "Can't remove the CRL file: %s\n", err ? err : "");
3275 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3276 return cli_dynerr(appctx, err);
3277}
3278
Remi Tricot-Le Bretoneef8e7b2021-04-20 17:42:02 +02003279/* parsing function of 'abort ssl crl-file' */
3280static int cli_parse_abort_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3281{
3282 char *err = NULL;
3283
3284 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3285 return 1;
3286
3287 if (!*args[3])
3288 return cli_err(appctx, "'abort ssl crl-file' expects a filename\n");
3289
3290 /* The operations on the CKCH architecture are locked so we can
3291 * manipulate ckch_store and ckch_inst */
3292 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3293 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
3294
3295 if (!crlfile_transaction.path) {
3296 memprintf(&err, "No ongoing transaction!\n");
3297 goto error;
3298 }
3299
3300 if (strcmp(crlfile_transaction.path, args[3]) != 0) {
3301 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", crlfile_transaction.path, args[3]);
3302 goto error;
3303 }
3304
3305 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
3306 ssl_store_delete_cafile_entry(crlfile_transaction.new_crlfile_entry);
3307 crlfile_transaction.new_crlfile_entry = NULL;
3308 crlfile_transaction.old_crlfile_entry = NULL;
3309 ha_free(&crlfile_transaction.path);
3310
3311 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3312
3313 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
3314 return cli_dynmsg(appctx, LOG_NOTICE, err);
3315
3316error:
3317 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3318
3319 return cli_dynerr(appctx, err);
3320}
3321
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003322
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003323/*
3324 * Display a Certificate Resignation List's information.
3325 * The information displayed is inspired by the output of 'openssl crl -in
3326 * crl.pem -text'.
3327 * Returns 0 in case of success.
3328 */
3329static int show_crl_detail(X509_CRL *crl, struct buffer *out)
3330{
3331 BIO *bio = NULL;
3332 struct buffer *tmp = alloc_trash_chunk();
3333 long version;
3334 X509_NAME *issuer;
3335 int write = -1;
3336 STACK_OF(X509_REVOKED) *rev = NULL;
3337 X509_REVOKED *rev_entry = NULL;
3338 int i;
3339
3340 if (!tmp)
3341 return -1;
3342
3343 if ((bio = BIO_new(BIO_s_mem())) == NULL)
3344 goto end;
3345
3346 /* Version (as displayed by 'openssl crl') */
3347 version = X509_CRL_get_version(crl);
3348 chunk_appendf(out, "Version %ld\n", version + 1);
3349
3350 /* Signature Algorithm */
3351 chunk_appendf(out, "Signature Algorithm: %s\n", OBJ_nid2ln(X509_CRL_get_signature_nid(crl)));
3352
3353 /* Issuer */
3354 chunk_appendf(out, "Issuer: ");
3355 if ((issuer = X509_CRL_get_issuer(crl)) == NULL)
3356 goto end;
3357 if ((ssl_sock_get_dn_oneline(issuer, tmp)) == -1)
3358 goto end;
3359 *(tmp->area + tmp->data) = '\0';
3360 chunk_appendf(out, "%s\n", tmp->area);
3361
3362 /* Last Update */
3363 chunk_appendf(out, "Last Update: ");
3364 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003365 if (BIO_reset(bio) == -1)
3366 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003367 if (ASN1_TIME_print(bio, X509_CRL_get0_lastUpdate(crl)) == 0)
3368 goto end;
3369 write = BIO_read(bio, tmp->area, tmp->size-1);
3370 tmp->area[write] = '\0';
3371 chunk_appendf(out, "%s\n", tmp->area);
3372
3373
3374 /* Next Update */
3375 chunk_appendf(out, "Next Update: ");
3376 chunk_reset(tmp);
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003377 if (BIO_reset(bio) == -1)
3378 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003379 if (ASN1_TIME_print(bio, X509_CRL_get0_nextUpdate(crl)) == 0)
3380 goto end;
3381 write = BIO_read(bio, tmp->area, tmp->size-1);
3382 tmp->area[write] = '\0';
3383 chunk_appendf(out, "%s\n", tmp->area);
3384
3385
3386 /* Revoked Certificates */
3387 rev = X509_CRL_get_REVOKED(crl);
3388 if (sk_X509_REVOKED_num(rev) > 0)
3389 chunk_appendf(out, "Revoked Certificates:\n");
3390 else
3391 chunk_appendf(out, "No Revoked Certificates.\n");
3392
3393 for (i = 0; i < sk_X509_REVOKED_num(rev); i++) {
3394 rev_entry = sk_X509_REVOKED_value(rev, i);
3395
3396 /* Serial Number and Revocation Date */
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +02003397 if (BIO_reset(bio) == -1)
3398 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003399 BIO_printf(bio , " Serial Number: ");
Remi Tricot-Le Breton18c7d832021-05-17 18:38:34 +02003400 i2a_ASN1_INTEGER(bio, (ASN1_INTEGER*)X509_REVOKED_get0_serialNumber(rev_entry));
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003401 BIO_printf(bio, "\n Revocation Date: ");
Remi Tricot-Le Bretona6b27842021-05-18 10:06:00 +02003402 if (ASN1_TIME_print(bio, X509_REVOKED_get0_revocationDate(rev_entry)) == 0)
3403 goto end;
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003404 BIO_printf(bio, "\n");
3405
3406 write = BIO_read(bio, tmp->area, tmp->size-1);
3407 tmp->area[write] = '\0';
3408 chunk_appendf(out, "%s", tmp->area);
3409 }
3410
3411end:
3412 free_trash_chunk(tmp);
3413 if (bio)
3414 BIO_free(bio);
3415
3416 return 0;
3417}
3418
3419/* IO handler of details "show ssl crl-file <filename[:index]>" */
3420static int cli_io_handler_show_crlfile_detail(struct appctx *appctx)
3421{
3422 struct stream_interface *si = appctx->owner;
3423 struct cafile_entry *cafile_entry = appctx->ctx.cli.p0;
3424 struct buffer *out = alloc_trash_chunk();
3425 int i;
3426 X509_CRL *crl;
3427 STACK_OF(X509_OBJECT) *objs;
3428 int retval = 0;
3429 long index = (long)appctx->ctx.cli.p1;
3430
3431 if (!out)
3432 goto end_no_putchk;
3433
3434 chunk_appendf(out, "Filename: ");
3435 if (cafile_entry == crlfile_transaction.new_crlfile_entry)
3436 chunk_appendf(out, "*");
3437 chunk_appendf(out, "%s\n", cafile_entry->path);
3438
3439 chunk_appendf(out, "Status: ");
3440 if (!cafile_entry->ca_store)
3441 chunk_appendf(out, "Empty\n");
3442 else if (LIST_ISEMPTY(&cafile_entry->ckch_inst_link))
3443 chunk_appendf(out, "Unused\n");
3444 else
3445 chunk_appendf(out, "Used\n");
3446
3447 if (!cafile_entry->ca_store)
3448 goto end;
3449
3450 objs = X509_STORE_get0_objects(cafile_entry->ca_store);
3451 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
3452 crl = X509_OBJECT_get0_X509_CRL(sk_X509_OBJECT_value(objs, i));
3453 if (!crl)
3454 continue;
3455
3456 /* CRL indexes start at 1 on the CLI output. */
3457 if (index && index-1 != i)
3458 continue;
3459
3460 chunk_appendf(out, "\nCertificate Revocation List #%d:\n", i+1);
3461 retval = show_crl_detail(crl, out);
3462 if (retval < 0)
3463 goto end_no_putchk;
3464 else if (retval || index)
3465 goto end;
3466 }
3467
3468end:
3469 if (ci_putchk(si_ic(si), out) == -1) {
3470 si_rx_room_blk(si);
3471 goto yield;
3472 }
3473
3474end_no_putchk:
3475 free_trash_chunk(out);
3476 return 1;
3477yield:
3478 free_trash_chunk(out);
3479 return 0; /* should come back */
3480}
3481
3482/* parsing function for 'show ssl crl-file [crlfile[:index]]' */
3483static int cli_parse_show_crlfile(char **args, char *payload, struct appctx *appctx, void *private)
3484{
3485 struct cafile_entry *cafile_entry;
3486 long index = 0;
3487 char *colons;
3488 char *err = NULL;
3489
3490 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3491 return cli_err(appctx, "Can't allocate memory!\n");
3492
3493 /* The operations on the CKCH architecture are locked so we can
3494 * manipulate ckch_store and ckch_inst */
3495 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
3496 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
3497
3498 /* check if there is a certificate to lookup */
3499 if (*args[3]) {
3500
3501 /* Look for an optional index after the CRL file name */
3502 colons = strchr(args[3], ':');
3503 if (colons) {
3504 char *endptr;
3505
3506 index = strtol(colons + 1, &endptr, 10);
3507 /* Indexes start at 1 */
3508 if (colons + 1 == endptr || *endptr != '\0' || index <= 0) {
3509 memprintf(&err, "wrong CRL index after colons in '%s'!", args[3]);
3510 goto error;
3511 }
3512 *colons = '\0';
3513 }
3514
3515 if (*args[3] == '*') {
3516 if (!crlfile_transaction.new_crlfile_entry)
3517 goto error;
3518
3519 cafile_entry = crlfile_transaction.new_crlfile_entry;
3520
3521 if (strcmp(args[3] + 1, cafile_entry->path) != 0)
3522 goto error;
3523
3524 } else {
3525 /* Get the "original" cafile_entry and not the
3526 * uncommitted one if it exists. */
3527 if ((cafile_entry = ssl_store_get_cafile_entry(args[3], 1)) == NULL || cafile_entry->type != CAFILE_CRL)
3528 goto error;
3529 }
3530
3531 appctx->ctx.cli.p0 = cafile_entry;
3532 appctx->ctx.cli.p1 = (void*)index;
3533 /* use the IO handler that shows details */
3534 appctx->io_handler = cli_io_handler_show_crlfile_detail;
3535 }
3536
3537 return 0;
3538
3539error:
3540 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3541 if (err)
3542 return cli_dynerr(appctx, err);
3543 return cli_err(appctx, "Can't display the CA file : Not found!\n");
3544}
3545
3546/* IO handler of "show ssl crl-file". The command taking a specific CRL file name
3547 * is managed in cli_io_handler_show_crlfile_detail. */
3548static int cli_io_handler_show_crlfile(struct appctx *appctx)
3549{
3550 struct buffer *trash = alloc_trash_chunk();
3551 struct ebmb_node *node;
3552 struct stream_interface *si = appctx->owner;
3553 struct cafile_entry *cafile_entry;
3554
3555 if (trash == NULL)
3556 return 1;
3557
3558 if (!appctx->ctx.ssl.old_crlfile_entry) {
3559 if (crlfile_transaction.old_crlfile_entry) {
3560 chunk_appendf(trash, "# transaction\n");
3561 chunk_appendf(trash, "*%s\n", crlfile_transaction.old_crlfile_entry->path);
3562 }
3563 }
3564
3565 /* First time in this io_handler. */
3566 if (!appctx->ctx.cli.p0) {
3567 chunk_appendf(trash, "# filename\n");
3568 node = ebmb_first(&cafile_tree);
3569 } else {
3570 /* We yielded during a previous call. */
3571 node = &((struct cafile_entry*)appctx->ctx.cli.p0)->node;
3572 }
3573
3574 while (node) {
3575 cafile_entry = ebmb_entry(node, struct cafile_entry, node);
3576 if (cafile_entry->type == CAFILE_CRL) {
3577 chunk_appendf(trash, "%s\n", cafile_entry->path);
3578 }
3579
3580 node = ebmb_next(node);
3581 if (ci_putchk(si_ic(si), trash) == -1) {
3582 si_rx_room_blk(si);
3583 goto yield;
3584 }
3585 }
3586
3587 appctx->ctx.cli.p0 = NULL;
3588 free_trash_chunk(trash);
3589 return 1;
3590yield:
3591
3592 free_trash_chunk(trash);
3593 appctx->ctx.cli.p0 = cafile_entry;
3594 return 0; /* should come back */
3595}
3596
3597
3598/* release function of the 'show ssl crl-file' command */
3599static void cli_release_show_crlfile(struct appctx *appctx)
3600{
3601 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
3602}
3603
3604
William Lallemandee8530c2020-06-23 18:19:42 +02003605void ckch_deinit()
3606{
3607 struct eb_node *node, *next;
3608 struct ckch_store *store;
3609
3610 node = eb_first(&ckchs_tree);
3611 while (node) {
3612 next = eb_next(node);
3613 store = ebmb_entry(node, struct ckch_store, node);
3614 ckch_store_free(store);
3615 node = next;
3616 }
3617}
William Lallemandda8584c2020-05-14 10:14:37 +02003618
3619/* register cli keywords */
3620static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003621 { { "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 },
3622 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
3623 { { "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 },
3624 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
3625 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
3626 { { "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 },
3627
Remi Tricot-Le Breton9f40fe02021-03-16 16:21:27 +01003628 { { "new", "ssl", "ca-file", NULL }, "new ssl cafile <cafile> : create a new CA file to be used in a crt-list", cli_parse_new_cafile, NULL, NULL },
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01003629 { { "set", "ssl", "ca-file", NULL }, "set ssl ca-file <cafile> <payload> : replace a CA file", cli_parse_set_cafile, NULL, NULL },
Remi Tricot-Le Bretona51b3392021-04-20 17:38:14 +02003630 { { "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 Bretond5fd09d2021-03-11 10:22:52 +01003631 { { "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 Bretonc3a84772021-03-25 18:13:57 +01003632 { { "del", "ssl", "ca-file", NULL }, "del ssl ca-file <cafile> : delete an unused CA file", cli_parse_del_cafile, NULL, NULL },
Remi Tricot-Le Breton2a22e162021-03-16 11:19:33 +01003633 { { "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 Bretona51b3392021-04-20 17:38:14 +02003634
Remi Tricot-Le Breton720e3b92021-04-26 11:00:42 +02003635 { { "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 Bretona51b3392021-04-20 17:38:14 +02003636 { { "set", "ssl", "crl-file", NULL }, "set ssl crl-file <crlfile> <payload> : replace a CRL file", cli_parse_set_crlfile, NULL, NULL },
3637 { { "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 Bretoneef8e7b2021-04-20 17:42:02 +02003638 { { "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 Breton720e3b92021-04-26 11:00:42 +02003639 { { "del", "ssl", "crl-file", NULL }, "del ssl crl-file <crlfile> : delete an unused CRL file", cli_parse_del_crlfile, NULL, NULL },
Remi Tricot-Le Breton51e28b62021-04-20 17:58:01 +02003640 { { "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 Lallemandda8584c2020-05-14 10:14:37 +02003641 { { NULL }, NULL, NULL, NULL }
3642}};
3643
3644INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3645