blob: 3f89ac3cc682f78064b18c213653b845aac998d3 [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
William Lallemand03c331c2020-05-13 10:10:01 +020053
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +010054
William Lallemand03c331c2020-05-13 10:10:01 +020055/******************** cert_key_and_chain functions *************************
56 * These are the functions that fills a cert_key_and_chain structure. For the
57 * functions filling a SSL_CTX from a cert_key_and_chain, see ssl_sock.c
58 */
59
60/*
61 * Try to parse Signed Certificate Timestamp List structure. This function
62 * makes only basic test if the data seems like SCTL. No signature validation
63 * is performed.
64 */
65static int ssl_sock_parse_sctl(struct buffer *sctl)
66{
67 int ret = 1;
68 int len, pos, sct_len;
69 unsigned char *data;
70
71 if (sctl->data < 2)
72 goto out;
73
74 data = (unsigned char *) sctl->area;
75 len = (data[0] << 8) | data[1];
76
77 if (len + 2 != sctl->data)
78 goto out;
79
80 data = data + 2;
81 pos = 0;
82 while (pos < len) {
83 if (len - pos < 2)
84 goto out;
85
86 sct_len = (data[pos] << 8) | data[pos + 1];
87 if (pos + sct_len + 2 > len)
88 goto out;
89
90 pos += sct_len + 2;
91 }
92
93 ret = 0;
94
95out:
96 return ret;
97}
98
99/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
100 * It fills the ckch->sctl buffer
101 * return 0 on success or != 0 on failure */
102int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
103{
104 int fd = -1;
105 int r = 0;
106 int ret = 1;
107 struct buffer tmp;
108 struct buffer *src;
109 struct buffer *sctl;
110
111 if (buf) {
William Lallemand8d673942021-01-27 14:58:51 +0100112 chunk_initstr(&tmp, buf);
William Lallemand03c331c2020-05-13 10:10:01 +0200113 src = &tmp;
114 } else {
115 fd = open(sctl_path, O_RDONLY);
116 if (fd == -1)
117 goto end;
118
119 trash.data = 0;
120 while (trash.data < trash.size) {
121 r = read(fd, trash.area + trash.data, trash.size - trash.data);
122 if (r < 0) {
123 if (errno == EINTR)
124 continue;
125 goto end;
126 }
127 else if (r == 0) {
128 break;
129 }
130 trash.data += r;
131 }
132 src = &trash;
133 }
134
135 ret = ssl_sock_parse_sctl(src);
136 if (ret)
137 goto end;
138
139 sctl = calloc(1, sizeof(*sctl));
140 if (!chunk_dup(sctl, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100141 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200142 goto end;
143 }
144 /* no error, fill ckch with new context, old context must be free */
145 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100146 ha_free(&ckch->sctl->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200147 free(ckch->sctl);
148 }
149 ckch->sctl = sctl;
150 ret = 0;
151end:
152 if (fd != -1)
153 close(fd);
154
155 return ret;
156}
157
158#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
159/*
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500160 * This function load the OCSP Response in DER format contained in file at
William Lallemand03c331c2020-05-13 10:10:01 +0200161 * path 'ocsp_path' or base64 in a buffer <buf>
162 *
163 * Returns 0 on success, 1 in error case.
164 */
165int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
166{
167 int fd = -1;
168 int r = 0;
169 int ret = 1;
170 struct buffer *ocsp_response;
171 struct buffer *src = NULL;
172
173 if (buf) {
174 int i, j;
175 /* if it's from a buffer it will be base64 */
176
177 /* remove \r and \n from the payload */
178 for (i = 0, j = 0; buf[i]; i++) {
179 if (buf[i] == '\r' || buf[i] == '\n')
180 continue;
181 buf[j++] = buf[i];
182 }
183 buf[j] = 0;
184
185 ret = base64dec(buf, j, trash.area, trash.size);
186 if (ret < 0) {
187 memprintf(err, "Error reading OCSP response in base64 format");
188 goto end;
189 }
190 trash.data = ret;
191 src = &trash;
192 } else {
193 fd = open(ocsp_path, O_RDONLY);
194 if (fd == -1) {
195 memprintf(err, "Error opening OCSP response file");
196 goto end;
197 }
198
199 trash.data = 0;
200 while (trash.data < trash.size) {
201 r = read(fd, trash.area + trash.data, trash.size - trash.data);
202 if (r < 0) {
203 if (errno == EINTR)
204 continue;
205
206 memprintf(err, "Error reading OCSP response from file");
207 goto end;
208 }
209 else if (r == 0) {
210 break;
211 }
212 trash.data += r;
213 }
214 close(fd);
215 fd = -1;
216 src = &trash;
217 }
218
219 ocsp_response = calloc(1, sizeof(*ocsp_response));
220 if (!chunk_dup(ocsp_response, src)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100221 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200222 goto end;
223 }
224 /* no error, fill ckch with new context, old context must be free */
225 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100226 ha_free(&ckch->ocsp_response->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200227 free(ckch->ocsp_response);
228 }
229 ckch->ocsp_response = ocsp_response;
230 ret = 0;
231end:
232 if (fd != -1)
233 close(fd);
234
235 return ret;
236}
237#endif
238
239/*
240 * Try to load in a ckch every files related to a ckch.
241 * (PEM, sctl, ocsp, issuer etc.)
242 *
243 * This function is only used to load files during the configuration parsing,
244 * it is not used with the CLI.
245 *
246 * This allows us to carry the contents of the file without having to read the
247 * file multiple times. The caller must call
248 * ssl_sock_free_cert_key_and_chain_contents.
249 *
250 * returns:
251 * 0 on Success
252 * 1 on SSL Failure
253 */
254int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
255{
William Lallemand8e8581e2020-10-20 17:36:46 +0200256 struct buffer *fp = NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200257 int ret = 1;
258
259 /* try to load the PEM */
260 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
261 goto end;
262 }
263
William Lallemand8e8581e2020-10-20 17:36:46 +0200264 fp = alloc_trash_chunk();
265 if (!fp) {
266 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
267 goto end;
268 }
269
270 if (!chunk_strcpy(fp, path) || (b_data(fp) > MAXPATHLEN)) {
271 memprintf(err, "%s '%s' filename too long'.\n",
272 err && *err ? *err : "", fp->area);
273 ret = 1;
274 goto end;
275 }
276
William Lallemand089c1382020-10-23 17:35:12 +0200277 /* remove the ".crt" extension */
William Lallemand8e8581e2020-10-20 17:36:46 +0200278 if (global_ssl.extra_files_noext) {
279 char *ext;
280
281 /* look for the extension */
282 if ((ext = strrchr(fp->area, '.'))) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200283
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100284 if (strcmp(ext, ".crt") == 0) {
William Lallemand8e8581e2020-10-20 17:36:46 +0200285 *ext = '\0';
William Lallemand089c1382020-10-23 17:35:12 +0200286 fp->data = strlen(fp->area);
287 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200288 }
289
290 }
291
William Lallemand03c331c2020-05-13 10:10:01 +0200292 /* try to load an external private key if it wasn't in the PEM */
293 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200294 struct stat st;
295
William Lallemand8e8581e2020-10-20 17:36:46 +0200296
297 if (!chunk_strcat(fp, ".key") || (b_data(fp) > MAXPATHLEN)) {
298 memprintf(err, "%s '%s' filename too long'.\n",
299 err && *err ? *err : "", fp->area);
300 ret = 1;
301 goto end;
302 }
303
304 if (stat(fp->area, &st) == 0) {
305 if (ssl_sock_load_key_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200306 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200307 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200308 goto end;
309 }
310 }
William Lallemand03c331c2020-05-13 10:10:01 +0200311
William Lallemand8e8581e2020-10-20 17:36:46 +0200312 if (ckch->key == NULL) {
313 memprintf(err, "%sNo Private Key found in '%s'.\n", err && *err ? *err : "", fp->area);
314 goto end;
315 }
316 /* remove the added extension */
317 *(fp->area + fp->data - strlen(".key")) = '\0';
318 b_sub(fp, strlen(".key"));
William Lallemand03c331c2020-05-13 10:10:01 +0200319 }
320
321 if (!X509_check_private_key(ckch->cert, ckch->key)) {
322 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
323 err && *err ? *err : "", path);
324 goto end;
325 }
326
Ilya Shipitsinc47d6762021-02-13 11:45:33 +0500327#ifdef HAVE_SSL_SCTL
William Lallemand03c331c2020-05-13 10:10:01 +0200328 /* try to load the sctl file */
329 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemand03c331c2020-05-13 10:10:01 +0200330 struct stat st;
331
William Lallemand8e8581e2020-10-20 17:36:46 +0200332 if (!chunk_strcat(fp, ".sctl") || b_data(fp) > MAXPATHLEN) {
333 memprintf(err, "%s '%s' filename too long'.\n",
334 err && *err ? *err : "", fp->area);
335 ret = 1;
336 goto end;
337 }
338
339 if (stat(fp->area, &st) == 0) {
340 if (ssl_sock_load_sctl_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200341 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200342 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200343 ret = 1;
344 goto end;
345 }
346 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200347 /* remove the added extension */
348 *(fp->area + fp->data - strlen(".sctl")) = '\0';
349 b_sub(fp, strlen(".sctl"));
William Lallemand03c331c2020-05-13 10:10:01 +0200350 }
351#endif
352
353 /* try to load an ocsp response file */
354 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand03c331c2020-05-13 10:10:01 +0200355 struct stat st;
356
William Lallemand8e8581e2020-10-20 17:36:46 +0200357 if (!chunk_strcat(fp, ".ocsp") || b_data(fp) > MAXPATHLEN) {
358 memprintf(err, "%s '%s' filename too long'.\n",
359 err && *err ? *err : "", fp->area);
360 ret = 1;
361 goto end;
362 }
363
364 if (stat(fp->area, &st) == 0) {
365 if (ssl_sock_load_ocsp_response_from_file(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200366 ret = 1;
367 goto end;
368 }
369 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200370 /* remove the added extension */
371 *(fp->area + fp->data - strlen(".ocsp")) = '\0';
372 b_sub(fp, strlen(".ocsp"));
William Lallemand03c331c2020-05-13 10:10:01 +0200373 }
374
375#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
376 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
377 /* if no issuer was found, try to load an issuer from the .issuer */
378 if (!ckch->ocsp_issuer) {
379 struct stat st;
William Lallemand8e8581e2020-10-20 17:36:46 +0200380
381 if (!chunk_strcat(fp, ".issuer") || b_data(fp) > MAXPATHLEN) {
382 memprintf(err, "%s '%s' filename too long'.\n",
383 err && *err ? *err : "", fp->area);
384 ret = 1;
385 goto end;
386 }
William Lallemand03c331c2020-05-13 10:10:01 +0200387
William Lallemand8e8581e2020-10-20 17:36:46 +0200388 if (stat(fp->area, &st) == 0) {
389 if (ssl_sock_load_issuer_file_into_ckch(fp->area, NULL, ckch, err)) {
William Lallemand03c331c2020-05-13 10:10:01 +0200390 ret = 1;
391 goto end;
392 }
393
394 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
395 memprintf(err, "%s '%s' is not an issuer'.\n",
William Lallemand8e8581e2020-10-20 17:36:46 +0200396 err && *err ? *err : "", fp->area);
William Lallemand03c331c2020-05-13 10:10:01 +0200397 ret = 1;
398 goto end;
399 }
400 }
William Lallemand8e8581e2020-10-20 17:36:46 +0200401 /* remove the added extension */
402 *(fp->area + fp->data - strlen(".issuer")) = '\0';
403 b_sub(fp, strlen(".issuer"));
William Lallemand03c331c2020-05-13 10:10:01 +0200404 }
405 }
406#endif
407
408 ret = 0;
409
410end:
411
412 ERR_clear_error();
413
414 /* Something went wrong in one of the reads */
415 if (ret != 0)
416 ssl_sock_free_cert_key_and_chain_contents(ckch);
417
William Lallemand8e8581e2020-10-20 17:36:46 +0200418 free_trash_chunk(fp);
419
William Lallemand03c331c2020-05-13 10:10:01 +0200420 return ret;
421}
422
423/*
424 * Try to load a private key file from a <path> or a buffer <buf>
425 *
426 * If it failed you should not attempt to use the ckch but free it.
427 *
428 * Return 0 on success or != 0 on failure
429 */
430int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
431{
432 BIO *in = NULL;
433 int ret = 1;
434 EVP_PKEY *key = NULL;
435
436 if (buf) {
437 /* reading from a buffer */
438 in = BIO_new_mem_buf(buf, -1);
439 if (in == NULL) {
440 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
441 goto end;
442 }
443
444 } else {
445 /* reading from a file */
446 in = BIO_new(BIO_s_file());
447 if (in == NULL)
448 goto end;
449
450 if (BIO_read_filename(in, path) <= 0)
451 goto end;
452 }
453
454 /* Read Private Key */
455 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
456 if (key == NULL) {
457 memprintf(err, "%sunable to load private key from file '%s'.\n",
458 err && *err ? *err : "", path);
459 goto end;
460 }
461
462 ret = 0;
463
464 SWAP(ckch->key, key);
465
466end:
467
468 ERR_clear_error();
469 if (in)
470 BIO_free(in);
471 if (key)
472 EVP_PKEY_free(key);
473
474 return ret;
475}
476
477/*
478 * Try to load a PEM file from a <path> or a buffer <buf>
479 * The PEM must contain at least a Certificate,
480 * It could contain a DH, a certificate chain and a PrivateKey.
481 *
482 * If it failed you should not attempt to use the ckch but free it.
483 *
484 * Return 0 on success or != 0 on failure
485 */
486int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
487{
488 BIO *in = NULL;
489 int ret = 1;
490 X509 *ca;
491 X509 *cert = NULL;
492 EVP_PKEY *key = NULL;
493 DH *dh = NULL;
494 STACK_OF(X509) *chain = NULL;
495
496 if (buf) {
497 /* reading from a buffer */
498 in = BIO_new_mem_buf(buf, -1);
499 if (in == NULL) {
500 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
501 goto end;
502 }
503
504 } else {
505 /* reading from a file */
506 in = BIO_new(BIO_s_file());
507 if (in == NULL) {
508 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
509 goto end;
510 }
511
512 if (BIO_read_filename(in, path) <= 0) {
513 memprintf(err, "%scannot open the file '%s'.\n",
514 err && *err ? *err : "", path);
515 goto end;
516 }
517 }
518
519 /* Read Private Key */
520 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
521 /* no need to check for errors here, because the private key could be loaded later */
522
523#ifndef OPENSSL_NO_DH
524 /* Seek back to beginning of file */
525 if (BIO_reset(in) == -1) {
526 memprintf(err, "%san error occurred while reading the file '%s'.\n",
527 err && *err ? *err : "", path);
528 goto end;
529 }
530
531 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
532 /* no need to return an error there, dh is not mandatory */
533#endif
534
535 /* Seek back to beginning of file */
536 if (BIO_reset(in) == -1) {
537 memprintf(err, "%san error occurred while reading the file '%s'.\n",
538 err && *err ? *err : "", path);
539 goto end;
540 }
541
542 /* Read Certificate */
543 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
544 if (cert == NULL) {
545 memprintf(err, "%sunable to load certificate from file '%s'.\n",
546 err && *err ? *err : "", path);
547 goto end;
548 }
549
550 /* Look for a Certificate Chain */
551 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
552 if (chain == NULL)
553 chain = sk_X509_new_null();
554 if (!sk_X509_push(chain, ca)) {
555 X509_free(ca);
556 goto end;
557 }
558 }
559
560 ret = ERR_get_error();
561 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
562 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
563 err && *err ? *err : "", path);
564 goto end;
565 }
566
567 /* once it loaded the PEM, it should remove everything else in the ckch */
568 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100569 ha_free(&ckch->ocsp_response->area);
570 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200571 }
572
573 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100574 ha_free(&ckch->sctl->area);
575 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200576 }
577
578 if (ckch->ocsp_issuer) {
579 X509_free(ckch->ocsp_issuer);
580 ckch->ocsp_issuer = NULL;
581 }
582
583 /* no error, fill ckch with new context, old context will be free at end: */
584 SWAP(ckch->key, key);
585 SWAP(ckch->dh, dh);
586 SWAP(ckch->cert, cert);
587 SWAP(ckch->chain, chain);
588
589 ret = 0;
590
591end:
592
593 ERR_clear_error();
594 if (in)
595 BIO_free(in);
596 if (key)
597 EVP_PKEY_free(key);
598 if (dh)
599 DH_free(dh);
600 if (cert)
601 X509_free(cert);
602 if (chain)
603 sk_X509_pop_free(chain, X509_free);
604
605 return ret;
606}
607
608/* Frees the contents of a cert_key_and_chain
609 */
610void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
611{
612 if (!ckch)
613 return;
614
615 /* Free the certificate and set pointer to NULL */
616 if (ckch->cert)
617 X509_free(ckch->cert);
618 ckch->cert = NULL;
619
620 /* Free the key and set pointer to NULL */
621 if (ckch->key)
622 EVP_PKEY_free(ckch->key);
623 ckch->key = NULL;
624
625 /* Free each certificate in the chain */
626 if (ckch->chain)
627 sk_X509_pop_free(ckch->chain, X509_free);
628 ckch->chain = NULL;
629
630 if (ckch->dh)
631 DH_free(ckch->dh);
632 ckch->dh = NULL;
633
634 if (ckch->sctl) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100635 ha_free(&ckch->sctl->area);
636 ha_free(&ckch->sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200637 }
638
639 if (ckch->ocsp_response) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100640 ha_free(&ckch->ocsp_response->area);
641 ha_free(&ckch->ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200642 }
643
644 if (ckch->ocsp_issuer)
645 X509_free(ckch->ocsp_issuer);
646 ckch->ocsp_issuer = NULL;
647}
648
649/*
650 *
651 * This function copy a cert_key_and_chain in memory
652 *
653 * It's used to try to apply changes on a ckch before committing them, because
654 * most of the time it's not possible to revert those changes
655 *
656 * Return a the dst or NULL
657 */
658struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
659 struct cert_key_and_chain *dst)
660{
William Lallemand6c096142021-02-23 14:45:45 +0100661 if (!src || !dst)
662 return NULL;
663
William Lallemand03c331c2020-05-13 10:10:01 +0200664 if (src->cert) {
665 dst->cert = src->cert;
666 X509_up_ref(src->cert);
667 }
668
669 if (src->key) {
670 dst->key = src->key;
671 EVP_PKEY_up_ref(src->key);
672 }
673
674 if (src->chain) {
675 dst->chain = X509_chain_up_ref(src->chain);
676 }
677
678 if (src->dh) {
679 DH_up_ref(src->dh);
680 dst->dh = src->dh;
681 }
682
683 if (src->sctl) {
684 struct buffer *sctl;
685
686 sctl = calloc(1, sizeof(*sctl));
687 if (!chunk_dup(sctl, src->sctl)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100688 ha_free(&sctl);
William Lallemand03c331c2020-05-13 10:10:01 +0200689 goto error;
690 }
691 dst->sctl = sctl;
692 }
693
694 if (src->ocsp_response) {
695 struct buffer *ocsp_response;
696
697 ocsp_response = calloc(1, sizeof(*ocsp_response));
698 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100699 ha_free(&ocsp_response);
William Lallemand03c331c2020-05-13 10:10:01 +0200700 goto error;
701 }
702 dst->ocsp_response = ocsp_response;
703 }
704
705 if (src->ocsp_issuer) {
706 X509_up_ref(src->ocsp_issuer);
707 dst->ocsp_issuer = src->ocsp_issuer;
708 }
709
710 return dst;
711
712error:
713
714 /* free everything */
715 ssl_sock_free_cert_key_and_chain_contents(dst);
716
717 return NULL;
718}
719
720/*
721 * return 0 on success or != 0 on failure
722 */
723int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
724{
725 int ret = 1;
726 BIO *in = NULL;
727 X509 *issuer;
728
729 if (buf) {
730 /* reading from a buffer */
731 in = BIO_new_mem_buf(buf, -1);
732 if (in == NULL) {
733 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
734 goto end;
735 }
736
737 } else {
738 /* reading from a file */
739 in = BIO_new(BIO_s_file());
740 if (in == NULL)
741 goto end;
742
743 if (BIO_read_filename(in, path) <= 0)
744 goto end;
745 }
746
747 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
748 if (!issuer) {
749 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
750 err && *err ? *err : "", path);
751 goto end;
752 }
753 /* no error, fill ckch with new context, old context must be free */
754 if (ckch->ocsp_issuer)
755 X509_free(ckch->ocsp_issuer);
756 ckch->ocsp_issuer = issuer;
757 ret = 0;
758
759end:
760
761 ERR_clear_error();
762 if (in)
763 BIO_free(in);
764
765 return ret;
766}
767
768/******************** ckch_store functions ***********************************
769 * The ckch_store is a structure used to cache and index the SSL files used in
770 * configuration
771 */
772
773/*
774 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
775 */
776void ckch_store_free(struct ckch_store *store)
777{
778 struct ckch_inst *inst, *inst_s;
779
780 if (!store)
781 return;
782
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200783 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200784
Willy Tarreau61cfdf42021-02-20 10:46:51 +0100785 ha_free(&store->ckch);
William Lallemand03c331c2020-05-13 10:10:01 +0200786
787 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
788 ckch_inst_free(inst);
789 }
790 ebmb_delete(&store->node);
791 free(store);
792}
793
794/*
795 * create and initialize a ckch_store
796 * <path> is the key name
797 * <nmemb> is the number of store->ckch objects to allocate
798 *
799 * Return a ckch_store or NULL upon failure.
800 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200801struct ckch_store *ckch_store_new(const char *filename)
William Lallemand03c331c2020-05-13 10:10:01 +0200802{
803 struct ckch_store *store;
804 int pathlen;
805
806 pathlen = strlen(filename);
807 store = calloc(1, sizeof(*store) + pathlen + 1);
808 if (!store)
809 return NULL;
810
William Lallemand03c331c2020-05-13 10:10:01 +0200811 memcpy(store->path, filename, pathlen + 1);
812
813 LIST_INIT(&store->ckch_inst);
814 LIST_INIT(&store->crtlist_entry);
815
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200816 store->ckch = calloc(1, sizeof(*store->ckch));
William Lallemand03c331c2020-05-13 10:10:01 +0200817 if (!store->ckch)
818 goto error;
819
820 return store;
821error:
822 ckch_store_free(store);
823 return NULL;
824}
825
826/* allocate and duplicate a ckch_store
827 * Return a new ckch_store or NULL */
828struct ckch_store *ckchs_dup(const struct ckch_store *src)
829{
830 struct ckch_store *dst;
831
William Lallemand6c096142021-02-23 14:45:45 +0100832 if (!src)
833 return NULL;
834
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200835 dst = ckch_store_new(src->path);
Eric Salama6ac61e32021-02-23 16:50:57 +0100836 if (!dst)
837 return NULL;
William Lallemand03c331c2020-05-13 10:10:01 +0200838
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200839 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
840 goto error;
William Lallemand03c331c2020-05-13 10:10:01 +0200841
842 return dst;
843
844error:
845 ckch_store_free(dst);
846
847 return NULL;
848}
849
850/*
851 * lookup a path into the ckchs tree.
852 */
853struct ckch_store *ckchs_lookup(char *path)
854{
855 struct ebmb_node *eb;
856
857 eb = ebst_lookup(&ckchs_tree, path);
858 if (!eb)
859 return NULL;
860
861 return ebmb_entry(eb, struct ckch_store, node);
862}
863
864/*
865 * This function allocate a ckch_store and populate it with certificates from files.
866 */
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200867struct ckch_store *ckchs_load_cert_file(char *path, char **err)
William Lallemand03c331c2020-05-13 10:10:01 +0200868{
869 struct ckch_store *ckchs;
870
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200871 ckchs = ckch_store_new(path);
William Lallemand03c331c2020-05-13 10:10:01 +0200872 if (!ckchs) {
873 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
874 goto end;
875 }
William Lallemand03c331c2020-05-13 10:10:01 +0200876
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200877 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
878 goto end;
William Lallemand03c331c2020-05-13 10:10:01 +0200879
William Lallemandbd8e6ed2020-09-16 16:08:08 +0200880 /* insert into the ckchs tree */
881 memcpy(ckchs->path, path, strlen(path) + 1);
882 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand03c331c2020-05-13 10:10:01 +0200883 return ckchs;
884
885end:
886 ckch_store_free(ckchs);
887
888 return NULL;
889}
890
William Lallemandfa1d8b42020-05-13 15:46:10 +0200891
892/******************** ckch_inst functions ******************************/
893
894/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
895/* The caller must use the lock of the bind_conf if used with inserted SNIs */
896void ckch_inst_free(struct ckch_inst *inst)
897{
898 struct sni_ctx *sni, *sni_s;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100899 struct ckch_inst_link_ref *link_ref, *link_ref_s;
William Lallemandfa1d8b42020-05-13 15:46:10 +0200900
901 if (inst == NULL)
902 return;
903
904 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
905 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +0200906 LIST_DELETE(&sni->by_ckch_inst);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200907 ebmb_delete(&sni->name);
908 free(sni);
909 }
Remi Tricot-Le Bretonf3eedfe2021-01-25 17:19:44 +0100910 SSL_CTX_free(inst->ctx);
911 inst->ctx = NULL;
Willy Tarreau2b718102021-04-21 07:32:39 +0200912 LIST_DELETE(&inst->by_ckchs);
913 LIST_DELETE(&inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100914
915 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
916 LIST_DELETE(&link_ref->link->list);
917 LIST_DELETE(&link_ref->list);
918 free(link_ref);
919 }
920
William Lallemandfa1d8b42020-05-13 15:46:10 +0200921 free(inst);
922}
923
924/* Alloc and init a ckch_inst */
925struct ckch_inst *ckch_inst_new()
926{
927 struct ckch_inst *ckch_inst;
928
929 ckch_inst = calloc(1, sizeof *ckch_inst);
930 if (!ckch_inst)
931 return NULL;
932
933 LIST_INIT(&ckch_inst->sni_ctx);
934 LIST_INIT(&ckch_inst->by_ckchs);
935 LIST_INIT(&ckch_inst->by_crtlist_entry);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100936 LIST_INIT(&ckch_inst->cafile_link_refs);
William Lallemandfa1d8b42020-05-13 15:46:10 +0200937
938 return ckch_inst;
939}
940
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200941
942/******************** ssl_store functions ******************************/
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100943struct eb_root cafile_tree = EB_ROOT;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200944
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100945/*
946 * Returns the cafile_entry found in the cafile_tree indexed by the path 'path'.
947 * If 'oldest_entry' is 1, returns the "original" cafile_entry (since
948 * during a set cafile/commit cafile cycle there might be two entries for any
949 * given path, the original one and the new one set via the CLI but not
950 * committed yet).
951 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100952struct cafile_entry *ssl_store_get_cafile_entry(char *path, int oldest_entry)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200953{
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100954 struct cafile_entry *ca_e = NULL;
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200955 struct ebmb_node *eb;
956
957 eb = ebst_lookup(&cafile_tree, path);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100958 while (eb) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200959 ca_e = ebmb_entry(eb, struct cafile_entry, node);
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100960 /* The ebst_lookup in a tree that has duplicates returns the
961 * oldest entry first. If we want the latest entry, we need to
962 * iterate over all the duplicates until we find the last one
963 * (in our case there should never be more than two entries for
964 * any given path). */
965 if (oldest_entry)
966 return ca_e;
967 eb = ebmb_next_dup(eb);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200968 }
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100969 return ca_e;
970}
971
Remi Tricot-Le Breton38c999b2021-02-23 16:28:43 +0100972int ssl_store_add_uncommitted_cafile_entry(struct cafile_entry *entry)
973{
974 return (ebst_insert(&cafile_tree, &entry->node) != &entry->node);
975}
976
Remi Tricot-Le Breton9f0c9362021-02-19 15:06:28 +0100977X509_STORE* ssl_store_get0_locations_file(char *path)
978{
979 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
980
981 if (ca_e)
982 return ca_e->ca_store;
983
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +0200984 return NULL;
985}
986
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +0100987/* Create a cafile_entry object, without adding it to the cafile_tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +0200988struct 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 +0100989{
990 struct cafile_entry *ca_e;
991 int pathlen;
992
993 pathlen = strlen(path);
994
995 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
996 if (ca_e) {
997 memcpy(ca_e->path, path, pathlen + 1);
998 ca_e->ca_store = store;
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +0200999 ca_e->type = type;
Remi Tricot-Le Breton5daff3c2021-02-22 15:54:55 +01001000 LIST_INIT(&ca_e->ckch_inst_link);
1001 }
1002 return ca_e;
1003}
1004
1005/* Delete a cafile_entry. The caller is responsible from removing this entry
1006 * from the cafile_tree first if is was previously added into it. */
1007void ssl_store_delete_cafile_entry(struct cafile_entry *ca_e)
1008{
1009 struct ckch_inst_link *link, *link_s;
1010 if (!ca_e)
1011 return;
1012
1013 X509_STORE_free(ca_e->ca_store);
1014
1015 list_for_each_entry_safe(link, link_s, &ca_e->ckch_inst_link, list) {
1016 struct ckch_inst *inst = link->ckch_inst;
1017 struct ckch_inst_link_ref *link_ref, *link_ref_s;
1018 list_for_each_entry_safe(link_ref, link_ref_s, &inst->cafile_link_refs, list) {
1019 if (link_ref->link == link) {
1020 LIST_DELETE(&link_ref->list);
1021 free(link_ref);
1022 break;
1023 }
1024 }
1025 LIST_DELETE(&link->list);
1026 free(link);
1027 }
1028
1029 free(ca_e);
1030}
1031
Remi Tricot-Le Breton383fb142021-02-22 18:26:14 +01001032/*
1033 * Build a cafile_entry out of a buffer instead of out of a file.
1034 * This function is used when the "commit ssl ca-file" cli command is used.
1035 * It can parse CERTIFICATE sections as well as CRL ones.
1036 * Returns 0 in case of success, 1 otherwise.
1037 */
1038int ssl_store_load_ca_from_buf(struct cafile_entry *ca_e, char *cert_buf)
1039{
1040 int retval = 0;
1041
1042 if (!ca_e)
1043 return 1;
1044
1045 if (!ca_e->ca_store) {
1046 ca_e->ca_store = X509_STORE_new();
1047 if (ca_e->ca_store) {
1048 BIO *bio = BIO_new_mem_buf(cert_buf, strlen(cert_buf));
1049 if (bio) {
1050 X509_INFO *info;
1051 int i;
1052 STACK_OF(X509_INFO) *infos = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL);
1053 if (!infos)
1054 {
1055 BIO_free(bio);
1056 return 1;
1057 }
1058
1059 for (i = 0; i < sk_X509_INFO_num(infos) && !retval; i++) {
1060 info = sk_X509_INFO_value(infos, i);
1061 /* X509_STORE_add_cert and X509_STORE_add_crl return 1 on success */
1062 if (info->x509) {
1063 retval = !X509_STORE_add_cert(ca_e->ca_store, info->x509);
1064 }
1065 if (!retval && info->crl) {
1066 retval = !X509_STORE_add_crl(ca_e->ca_store, info->crl);
1067 }
1068 }
1069 retval = retval || (i != sk_X509_INFO_num(infos));
1070
1071 /* Cleanup */
1072 sk_X509_INFO_pop_free(infos, X509_INFO_free);
1073 BIO_free(bio);
1074 }
1075 }
1076 }
1077
1078 return retval;
1079}
1080
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001081int ssl_store_load_locations_file(char *path, int create_if_none, enum cafile_type type)
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001082{
1083 X509_STORE *store = ssl_store_get0_locations_file(path);
1084
1085 /* If this function is called by the CLI, we should not call the
1086 * X509_STORE_load_locations function because it performs forbidden disk
1087 * accesses. */
1088 if (!store && create_if_none) {
1089 struct cafile_entry *ca_e;
1090 store = X509_STORE_new();
1091 if (X509_STORE_load_locations(store, path, NULL)) {
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02001092 ca_e = ssl_store_create_cafile_entry(path, store, type);
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001093 if (ca_e) {
Remi Tricot-Le Bretonaf8820a2021-04-13 10:10:37 +02001094 ebst_insert(&cafile_tree, &ca_e->node);
1095 }
1096 } else {
1097 X509_STORE_free(store);
1098 store = NULL;
1099 }
1100 }
1101 return (store != NULL);
1102}
1103
1104
William Lallemandda8584c2020-05-14 10:14:37 +02001105/*************************** CLI commands ***********************/
1106
1107/* Type of SSL payloads that can be updated over the CLI */
1108
1109enum {
1110 CERT_TYPE_PEM = 0,
1111 CERT_TYPE_KEY,
1112#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1113 CERT_TYPE_OCSP,
1114#endif
1115 CERT_TYPE_ISSUER,
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001116#ifdef HAVE_SSL_SCTL
William Lallemandda8584c2020-05-14 10:14:37 +02001117 CERT_TYPE_SCTL,
1118#endif
1119 CERT_TYPE_MAX,
1120};
1121
1122struct {
1123 const char *ext;
1124 int type;
1125 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
1126 /* add a parsing callback */
1127} cert_exts[CERT_TYPE_MAX+1] = {
1128 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
1129 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
1130#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
1131 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
1132#endif
Ilya Shipitsinc47d6762021-02-13 11:45:33 +05001133#ifdef HAVE_SSL_SCTL
William Lallemandda8584c2020-05-14 10:14:37 +02001134 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
1135#endif
1136 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
1137 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
1138};
1139
1140
1141/* release function of the `show ssl cert' command */
1142static void cli_release_show_cert(struct appctx *appctx)
1143{
1144 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1145}
1146
1147/* IO handler of "show ssl cert <filename>" */
1148static int cli_io_handler_show_cert(struct appctx *appctx)
1149{
1150 struct buffer *trash = alloc_trash_chunk();
1151 struct ebmb_node *node;
1152 struct stream_interface *si = appctx->owner;
1153 struct ckch_store *ckchs;
1154
1155 if (trash == NULL)
1156 return 1;
1157
1158 if (!appctx->ctx.ssl.old_ckchs) {
1159 if (ckchs_transaction.old_ckchs) {
1160 ckchs = ckchs_transaction.old_ckchs;
1161 chunk_appendf(trash, "# transaction\n");
William Lallemand5685ccf2020-09-16 16:12:25 +02001162 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001163 }
1164 }
1165
1166 if (!appctx->ctx.cli.p0) {
1167 chunk_appendf(trash, "# filename\n");
1168 node = ebmb_first(&ckchs_tree);
1169 } else {
1170 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
1171 }
1172 while (node) {
1173 ckchs = ebmb_entry(node, struct ckch_store, node);
William Lallemand5685ccf2020-09-16 16:12:25 +02001174 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001175
1176 node = ebmb_next(node);
1177 if (ci_putchk(si_ic(si), trash) == -1) {
1178 si_rx_room_blk(si);
1179 goto yield;
1180 }
1181 }
1182
1183 appctx->ctx.cli.p0 = NULL;
1184 free_trash_chunk(trash);
1185 return 1;
1186yield:
1187
1188 free_trash_chunk(trash);
1189 appctx->ctx.cli.p0 = ckchs;
1190 return 0; /* should come back */
1191}
1192
1193/*
1194 * Extract and format the DNS SAN extensions and copy result into a chuink
1195 * Return 0;
1196 */
1197#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1198static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
1199{
1200 int i;
1201 char *str;
1202 STACK_OF(GENERAL_NAME) *names = NULL;
1203
1204 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
1205 if (names) {
1206 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
1207 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
1208 if (i > 0)
1209 chunk_appendf(out, ", ");
1210 if (name->type == GEN_DNS) {
1211 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
1212 chunk_appendf(out, "DNS:%s", str);
1213 OPENSSL_free(str);
1214 }
1215 }
1216 }
1217 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
1218 }
1219 return 0;
1220}
1221#endif
1222
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01001223/*
1224 * Build the ckch_inst_link that will be chained in the CA file entry and the
1225 * corresponding ckch_inst_link_ref that will be chained in the ckch instance.
1226 * Return 0 in case of success.
1227 */
1228static int do_chain_inst_and_cafile(struct cafile_entry *cafile_entry, struct ckch_inst *ckch_inst)
1229{
1230 struct ckch_inst_link *new_link;
1231 if (!LIST_ISEMPTY(&cafile_entry->ckch_inst_link)) {
1232 struct ckch_inst_link *link = LIST_ELEM(cafile_entry->ckch_inst_link.n,
1233 typeof(link), list);
1234 /* Do not add multiple references to the same
1235 * instance in a cafile_entry */
1236 if (link->ckch_inst == ckch_inst) {
1237 return 1;
1238 }
1239 }
1240
1241 new_link = calloc(1, sizeof(*new_link));
1242 if (new_link) {
1243 struct ckch_inst_link_ref *new_link_ref = calloc(1, sizeof(*new_link_ref));
1244 if (!new_link_ref) {
1245 free(new_link);
1246 return 1;
1247 }
1248
1249 new_link->ckch_inst = ckch_inst;
1250 new_link_ref->link = new_link;
1251 LIST_INIT(&new_link->list);
1252 LIST_INIT(&new_link_ref->list);
1253
1254 LIST_APPEND(&cafile_entry->ckch_inst_link, &new_link->list);
1255 LIST_APPEND(&ckch_inst->cafile_link_refs, &new_link_ref->list);
1256 }
1257
1258 return 0;
1259}
1260
1261
1262/*
1263 * Link a CA file tree entry to the ckch instance that uses it.
1264 * To determine if and which CA file tree entries need to be linked to the
1265 * instance, we follow the same logic performed in ssl_sock_prepare_ctx when
1266 * processing the verify option.
1267 * This function works for a frontend as well as for a backend, depending on the
1268 * configuration parameters given (bind_conf or server).
1269 */
1270void ckch_inst_add_cafile_link(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf,
1271 struct ssl_bind_conf *ssl_conf, const struct server *srv)
1272{
1273 int verify = SSL_VERIFY_NONE;
1274
1275 if (srv) {
1276
1277 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
1278 verify = SSL_VERIFY_PEER;
1279 switch (srv->ssl_ctx.verify) {
1280 case SSL_SOCK_VERIFY_NONE:
1281 verify = SSL_VERIFY_NONE;
1282 break;
1283 case SSL_SOCK_VERIFY_REQUIRED:
1284 verify = SSL_VERIFY_PEER;
1285 break;
1286 }
1287 }
1288 else {
1289 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
1290 case SSL_SOCK_VERIFY_NONE:
1291 verify = SSL_VERIFY_NONE;
1292 break;
1293 case SSL_SOCK_VERIFY_OPTIONAL:
1294 verify = SSL_VERIFY_PEER;
1295 break;
1296 case SSL_SOCK_VERIFY_REQUIRED:
1297 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1298 break;
1299 }
1300 }
1301
1302 if (verify & SSL_VERIFY_PEER) {
1303 struct cafile_entry *ca_file_entry = NULL;
1304 struct cafile_entry *ca_verify_file_entry = NULL;
1305 if (srv) {
1306 if (srv->ssl_ctx.ca_file) {
1307 ca_file_entry = ssl_store_get_cafile_entry(srv->ssl_ctx.ca_file, 0);
1308
1309 }
1310 }
1311 else {
1312 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
1313 char *ca_verify_file = (ssl_conf && ssl_conf->ca_verify_file) ? ssl_conf->ca_verify_file : bind_conf->ssl_conf.ca_verify_file;
1314
1315 if (ca_file)
1316 ca_file_entry = ssl_store_get_cafile_entry(ca_file, 0);
1317 if (ca_verify_file)
1318 ca_verify_file_entry = ssl_store_get_cafile_entry(ca_verify_file, 0);
1319 }
1320
1321 if (ca_file_entry) {
1322 /* If we have a ckch instance that is not already in the
1323 * cafile_entry's list, add it to it. */
1324 if (do_chain_inst_and_cafile(ca_file_entry, ckch_inst))
1325 return;
1326
1327 }
1328 if (ca_verify_file_entry && (ca_file_entry != ca_verify_file_entry)) {
1329 /* If we have a ckch instance that is not already in the
1330 * cafile_entry's list, add it to it. */
1331 if (do_chain_inst_and_cafile(ca_verify_file_entry, ckch_inst))
1332 return;
1333 }
1334 }
1335}
1336
William Lallemandda8584c2020-05-14 10:14:37 +02001337
1338
1339
1340/* IO handler of the details "show ssl cert <filename>" */
1341static int cli_io_handler_show_cert_detail(struct appctx *appctx)
1342{
1343 struct stream_interface *si = appctx->owner;
1344 struct ckch_store *ckchs = appctx->ctx.cli.p0;
1345 struct buffer *out = alloc_trash_chunk();
1346 struct buffer *tmp = alloc_trash_chunk();
1347 X509_NAME *name = NULL;
1348 STACK_OF(X509) *chain;
1349 unsigned int len = 0;
1350 int write = -1;
1351 BIO *bio = NULL;
1352 int i;
1353
1354 if (!tmp || !out)
1355 goto end_no_putchk;
1356
William Lallemand5685ccf2020-09-16 16:12:25 +02001357 chunk_appendf(out, "Filename: ");
1358 if (ckchs == ckchs_transaction.new_ckchs)
1359 chunk_appendf(out, "*");
1360 chunk_appendf(out, "%s\n", ckchs->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001361
William Lallemand5685ccf2020-09-16 16:12:25 +02001362 chunk_appendf(out, "Status: ");
1363 if (ckchs->ckch->cert == NULL)
1364 chunk_appendf(out, "Empty\n");
1365 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
1366 chunk_appendf(out, "Unused\n");
1367 else
1368 chunk_appendf(out, "Used\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001369
William Lallemand5685ccf2020-09-16 16:12:25 +02001370 if (ckchs->ckch->cert == NULL)
1371 goto end;
William Lallemandda8584c2020-05-14 10:14:37 +02001372
William Lallemand5685ccf2020-09-16 16:12:25 +02001373 chain = ckchs->ckch->chain;
1374 if (chain == NULL) {
1375 struct issuer_chain *issuer;
1376 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
1377 if (issuer) {
1378 chain = issuer->chain;
1379 chunk_appendf(out, "Chain Filename: ");
1380 chunk_appendf(out, "%s\n", issuer->path);
William Lallemandda8584c2020-05-14 10:14:37 +02001381 }
William Lallemand5685ccf2020-09-16 16:12:25 +02001382 }
1383 chunk_appendf(out, "Serial: ");
1384 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
1385 goto end;
1386 dump_binary(out, tmp->area, tmp->data);
1387 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001388
William Lallemand5685ccf2020-09-16 16:12:25 +02001389 chunk_appendf(out, "notBefore: ");
1390 chunk_reset(tmp);
1391 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1392 goto end;
1393 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
1394 goto end;
1395 write = BIO_read(bio, tmp->area, tmp->size-1);
1396 tmp->area[write] = '\0';
1397 BIO_free(bio);
1398 bio = NULL;
1399 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001400
William Lallemand5685ccf2020-09-16 16:12:25 +02001401 chunk_appendf(out, "notAfter: ");
1402 chunk_reset(tmp);
1403 if ((bio = BIO_new(BIO_s_mem())) == NULL)
1404 goto end;
1405 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
1406 goto end;
1407 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
1408 goto end;
1409 tmp->area[write] = '\0';
1410 BIO_free(bio);
1411 bio = NULL;
1412 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001413
1414#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand5685ccf2020-09-16 16:12:25 +02001415 chunk_appendf(out, "Subject Alternative Name: ");
1416 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
1417 goto end;
1418 *(out->area + out->data) = '\0';
1419 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001420#endif
William Lallemand5685ccf2020-09-16 16:12:25 +02001421 chunk_reset(tmp);
1422 chunk_appendf(out, "Algorithm: ");
1423 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
1424 goto end;
1425 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001426
William Lallemand5685ccf2020-09-16 16:12:25 +02001427 chunk_reset(tmp);
1428 chunk_appendf(out, "SHA1 FingerPrint: ");
1429 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
1430 goto end;
1431 tmp->data = len;
1432 dump_binary(out, tmp->area, tmp->data);
1433 chunk_appendf(out, "\n");
William Lallemandda8584c2020-05-14 10:14:37 +02001434
William Lallemand5685ccf2020-09-16 16:12:25 +02001435 chunk_appendf(out, "Subject: ");
1436 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
1437 goto end;
1438 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1439 goto end;
1440 *(tmp->area + tmp->data) = '\0';
1441 chunk_appendf(out, "%s\n", tmp->area);
1442
1443 chunk_appendf(out, "Issuer: ");
1444 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
1445 goto end;
1446 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1447 goto end;
1448 *(tmp->area + tmp->data) = '\0';
1449 chunk_appendf(out, "%s\n", tmp->area);
1450
1451 /* Displays subject of each certificate in the chain */
1452 for (i = 0; i < sk_X509_num(chain); i++) {
1453 X509 *ca = sk_X509_value(chain, i);
1454
1455 chunk_appendf(out, "Chain Subject: ");
1456 if ((name = X509_get_subject_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001457 goto end;
1458 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1459 goto end;
1460 *(tmp->area + tmp->data) = '\0';
1461 chunk_appendf(out, "%s\n", tmp->area);
1462
William Lallemand5685ccf2020-09-16 16:12:25 +02001463 chunk_appendf(out, "Chain Issuer: ");
1464 if ((name = X509_get_issuer_name(ca)) == NULL)
William Lallemandda8584c2020-05-14 10:14:37 +02001465 goto end;
1466 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
1467 goto end;
1468 *(tmp->area + tmp->data) = '\0';
1469 chunk_appendf(out, "%s\n", tmp->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001470 }
1471
1472end:
1473 if (ci_putchk(si_ic(si), out) == -1) {
1474 si_rx_room_blk(si);
1475 goto yield;
1476 }
1477
1478end_no_putchk:
1479 if (bio)
1480 BIO_free(bio);
1481 free_trash_chunk(tmp);
1482 free_trash_chunk(out);
1483 return 1;
1484yield:
1485 free_trash_chunk(tmp);
1486 free_trash_chunk(out);
1487 return 0; /* should come back */
1488}
1489
1490/* parsing function for 'show ssl cert [certfile]' */
1491static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
1492{
1493 struct ckch_store *ckchs;
1494
1495 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
1496 return cli_err(appctx, "Can't allocate memory!\n");
1497
1498 /* The operations on the CKCH architecture are locked so we can
1499 * manipulate ckch_store and ckch_inst */
1500 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1501 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
1502
1503 /* check if there is a certificate to lookup */
1504 if (*args[3]) {
1505 if (*args[3] == '*') {
1506 if (!ckchs_transaction.new_ckchs)
1507 goto error;
1508
1509 ckchs = ckchs_transaction.new_ckchs;
1510
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001511 if (strcmp(args[3] + 1, ckchs->path) != 0)
William Lallemandda8584c2020-05-14 10:14:37 +02001512 goto error;
1513
1514 } else {
1515 if ((ckchs = ckchs_lookup(args[3])) == NULL)
1516 goto error;
1517
1518 }
1519
William Lallemandda8584c2020-05-14 10:14:37 +02001520 appctx->ctx.cli.p0 = ckchs;
1521 /* use the IO handler that shows details */
1522 appctx->io_handler = cli_io_handler_show_cert_detail;
1523 }
1524
1525 return 0;
1526
1527error:
1528 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1529 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
1530}
1531
1532/* release function of the `set ssl cert' command, free things and unlock the spinlock */
1533static void cli_release_commit_cert(struct appctx *appctx)
1534{
1535 struct ckch_store *new_ckchs;
1536
1537 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1538
1539 if (appctx->st2 != SETCERT_ST_FIN) {
1540 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
1541 new_ckchs = appctx->ctx.ssl.new_ckchs;
1542
1543 /* if the allocation failed, we need to free everything from the temporary list */
1544 ckch_store_free(new_ckchs);
1545 }
1546}
1547
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001548
1549/*
1550 * Rebuild a new instance 'new_inst' based on an old instance 'ckchi' and a
1551 * specific ckch_store.
1552 * Returns 0 in case of success, 1 otherwise.
1553 */
1554static int ckch_inst_rebuild(struct ckch_store *ckch_store, struct ckch_inst *ckchi,
1555 struct ckch_inst **new_inst, char **err)
1556{
1557 int retval = 0;
1558 int errcode = 0;
1559 struct sni_ctx *sc0, *sc0s;
1560 char **sni_filter = NULL;
1561 int fcount = 0;
1562
1563 if (ckchi->crtlist_entry) {
1564 sni_filter = ckchi->crtlist_entry->filters;
1565 fcount = ckchi->crtlist_entry->fcount;
1566 }
1567
1568 if (ckchi->is_server_instance)
1569 errcode |= ckch_inst_new_load_srv_store(ckch_store->path, ckch_store, new_inst, err);
1570 else
1571 errcode |= ckch_inst_new_load_store(ckch_store->path, ckch_store, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, new_inst, err);
1572
1573 if (errcode & ERR_CODE)
1574 return 1;
1575
1576 /* if the previous ckchi was used as the default */
1577 if (ckchi->is_default)
1578 (*new_inst)->is_default = 1;
1579
1580 (*new_inst)->is_server_instance = ckchi->is_server_instance;
1581 (*new_inst)->server = ckchi->server;
1582 /* Create a new SSL_CTX and link it to the new instance. */
1583 if ((*new_inst)->is_server_instance) {
1584 retval = ssl_sock_prep_srv_ctx_and_inst(ckchi->server, (*new_inst)->ctx, (*new_inst));
1585 if (retval)
1586 return 1;
1587 }
1588
1589 /* create the link to the crtlist_entry */
1590 (*new_inst)->crtlist_entry = ckchi->crtlist_entry;
1591
1592 /* we need to initialize the SSL_CTX generated */
1593 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
1594 list_for_each_entry_safe(sc0, sc0s, &(*new_inst)->sni_ctx, by_ckch_inst) {
1595 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
1596 errcode |= ssl_sock_prep_ctx_and_inst(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, *new_inst, err);
1597 if (errcode & ERR_CODE)
1598 return 1;
1599 }
1600 }
1601
1602 return 0;
1603}
1604
1605/*
1606 * Load all the new SNIs of a newly built ckch instance in the trees, or replace
1607 * a server's main ckch instance.
1608 */
1609static void __ssl_sock_load_new_ckch_instance(struct ckch_inst *ckchi)
1610{
1611 /* The bind_conf will be null on server ckch_instances. */
1612 if (ckchi->is_server_instance) {
1613 int i;
1614 /* a lock is needed here since we have to free the SSL cache */
1615 HA_RWLOCK_WRLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1616 /* free the server current SSL_CTX */
1617 SSL_CTX_free(ckchi->server->ssl_ctx.ctx);
1618 /* Actual ssl context update */
1619 SSL_CTX_up_ref(ckchi->ctx);
1620 ckchi->server->ssl_ctx.ctx = ckchi->ctx;
1621 ckchi->server->ssl_ctx.inst = ckchi;
1622
1623 /* flush the session cache of the server */
1624 for (i = 0; i < global.nbthread; i++) {
1625 ha_free(&ckchi->server->ssl_ctx.reused_sess[i].ptr);
1626 }
1627 HA_RWLOCK_WRUNLOCK(SSL_SERVER_LOCK, &ckchi->server->ssl_ctx.lock);
1628
1629 } else {
1630 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1631 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
1632 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
1633 }
1634}
1635
1636/*
1637 * Delete a ckch instance that was replaced after a CLI command.
1638 */
1639static void __ckch_inst_free_locked(struct ckch_inst *ckchi)
1640{
1641 if (ckchi->is_server_instance) {
1642 /* no lock for servers */
1643 ckch_inst_free(ckchi);
1644 } else {
1645 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
1646
1647 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
1648 ckch_inst_free(ckchi);
1649 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
1650 }
1651}
1652
1653
William Lallemandda8584c2020-05-14 10:14:37 +02001654/*
1655 * This function tries to create the new ckch_inst and their SNIs
1656 */
1657static int cli_io_handler_commit_cert(struct appctx *appctx)
1658{
1659 struct stream_interface *si = appctx->owner;
1660 int y = 0;
1661 char *err = NULL;
1662 int errcode = 0;
1663 struct ckch_store *old_ckchs, *new_ckchs = NULL;
1664 struct ckch_inst *ckchi, *ckchis;
1665 struct buffer *trash = alloc_trash_chunk();
William Lallemandda8584c2020-05-14 10:14:37 +02001666 struct crtlist_entry *entry;
1667
1668 if (trash == NULL)
1669 goto error;
1670
1671 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
1672 goto error;
1673
1674 while (1) {
1675 switch (appctx->st2) {
1676 case SETCERT_ST_INIT:
1677 /* This state just print the update message */
1678 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
1679 if (ci_putchk(si_ic(si), trash) == -1) {
1680 si_rx_room_blk(si);
1681 goto yield;
1682 }
1683 appctx->st2 = SETCERT_ST_GEN;
1684 /* fallthrough */
1685 case SETCERT_ST_GEN:
1686 /*
1687 * This state generates the ckch instances with their
1688 * sni_ctxs and SSL_CTX.
1689 *
1690 * Since the SSL_CTX generation can be CPU consumer, we
1691 * yield every 10 instances.
1692 */
1693
1694 old_ckchs = appctx->ctx.ssl.old_ckchs;
1695 new_ckchs = appctx->ctx.ssl.new_ckchs;
1696
1697 if (!new_ckchs)
1698 continue;
1699
1700 /* get the next ckchi to regenerate */
1701 ckchi = appctx->ctx.ssl.next_ckchi;
1702 /* we didn't start yet, set it to the first elem */
1703 if (ckchi == NULL)
1704 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
1705
1706 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
1707 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
1708 struct ckch_inst *new_inst;
William Lallemandda8584c2020-05-14 10:14:37 +02001709
1710 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
1711 if (y >= 10) {
1712 /* save the next ckchi to compute */
1713 appctx->ctx.ssl.next_ckchi = ckchi;
1714 goto yield;
1715 }
1716
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001717 if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err))
William Lallemandda8584c2020-05-14 10:14:37 +02001718 goto error;
1719
William Lallemandda8584c2020-05-14 10:14:37 +02001720 /* display one dot per new instance */
1721 chunk_appendf(trash, ".");
1722 /* link the new ckch_inst to the duplicate */
Willy Tarreau2b718102021-04-21 07:32:39 +02001723 LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
William Lallemandda8584c2020-05-14 10:14:37 +02001724 y++;
1725 }
1726 appctx->st2 = SETCERT_ST_INSERT;
1727 /* fallthrough */
1728 case SETCERT_ST_INSERT:
1729 /* The generation is finished, we can insert everything */
1730
1731 old_ckchs = appctx->ctx.ssl.old_ckchs;
1732 new_ckchs = appctx->ctx.ssl.new_ckchs;
1733
1734 if (!new_ckchs)
1735 continue;
1736
1737 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
1738 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
1739 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
1740 ebpt_delete(&entry->node);
1741 /* change the ptr and reinsert the node */
1742 entry->node.key = new_ckchs;
1743 ebpt_insert(&entry->crtlist->entries, &entry->node);
1744 }
1745
William Lallemanda55685b2020-12-15 14:57:46 +01001746 /* insert the new ckch_insts in the crtlist_entry */
1747 list_for_each_entry(ckchi, &new_ckchs->ckch_inst, by_ckchs) {
1748 if (ckchi->crtlist_entry)
Willy Tarreau2b718102021-04-21 07:32:39 +02001749 LIST_INSERT(&ckchi->crtlist_entry->ckch_inst, &ckchi->by_crtlist_entry);
William Lallemanda55685b2020-12-15 14:57:46 +01001750 }
1751
William Lallemandda8584c2020-05-14 10:14:37 +02001752 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
1753 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001754 __ssl_sock_load_new_ckch_instance(ckchi);
William Lallemandda8584c2020-05-14 10:14:37 +02001755 }
1756
1757 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
1758 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
Remi Tricot-Le Bretonbfadc022021-02-24 12:20:48 +01001759 __ckch_inst_free_locked(ckchi);
William Lallemandda8584c2020-05-14 10:14:37 +02001760 }
1761
1762 /* Replace the old ckchs by the new one */
1763 ckch_store_free(old_ckchs);
1764 ebst_insert(&ckchs_tree, &new_ckchs->node);
1765 appctx->st2 = SETCERT_ST_FIN;
1766 /* fallthrough */
1767 case SETCERT_ST_FIN:
1768 /* we achieved the transaction, we can set everything to NULL */
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001769 ha_free(&ckchs_transaction.path);
William Lallemandda8584c2020-05-14 10:14:37 +02001770 ckchs_transaction.new_ckchs = NULL;
1771 ckchs_transaction.old_ckchs = NULL;
1772 goto end;
1773 }
1774 }
1775end:
1776
1777 chunk_appendf(trash, "\n");
1778 if (errcode & ERR_WARN)
1779 chunk_appendf(trash, "%s", err);
1780 chunk_appendf(trash, "Success!\n");
1781 if (ci_putchk(si_ic(si), trash) == -1)
1782 si_rx_room_blk(si);
1783 free_trash_chunk(trash);
1784 /* success: call the release function and don't come back */
1785 return 1;
1786yield:
1787 /* store the state */
1788 if (ci_putchk(si_ic(si), trash) == -1)
1789 si_rx_room_blk(si);
1790 free_trash_chunk(trash);
1791 si_rx_endp_more(si); /* let's come back later */
1792 return 0; /* should come back */
1793
1794error:
1795 /* spin unlock and free are done in the release function */
1796 if (trash) {
1797 chunk_appendf(trash, "\n%sFailed!\n", err);
1798 if (ci_putchk(si_ic(si), trash) == -1)
1799 si_rx_room_blk(si);
1800 free_trash_chunk(trash);
1801 }
1802 /* error: call the release function and don't come back */
1803 return 1;
1804}
1805
1806/*
1807 * Parsing function of 'commit ssl cert'
1808 */
1809static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
1810{
1811 char *err = NULL;
1812
1813 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1814 return 1;
1815
1816 if (!*args[3])
1817 return cli_err(appctx, "'commit ssl cert expects a filename\n");
1818
1819 /* The operations on the CKCH architecture are locked so we can
1820 * manipulate ckch_store and ckch_inst */
1821 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1822 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
1823
1824 if (!ckchs_transaction.path) {
1825 memprintf(&err, "No ongoing transaction! !\n");
1826 goto error;
1827 }
1828
1829 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
1830 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
1831 goto error;
1832 }
1833
William Lallemand5685ccf2020-09-16 16:12:25 +02001834 /* if a certificate is here, a private key must be here too */
1835 if (ckchs_transaction.new_ckchs->ckch->cert && !ckchs_transaction.new_ckchs->ckch->key) {
1836 memprintf(&err, "The transaction must contain at least a certificate and a private key!\n");
1837 goto error;
1838 }
William Lallemanda9419522020-06-24 16:26:41 +02001839
William Lallemand5685ccf2020-09-16 16:12:25 +02001840 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
1841 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
1842 goto error;
William Lallemandda8584c2020-05-14 10:14:37 +02001843 }
1844
1845 /* init the appctx structure */
1846 appctx->st2 = SETCERT_ST_INIT;
1847 appctx->ctx.ssl.next_ckchi = NULL;
1848 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
1849 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
1850
1851 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
1852 return 0;
1853
1854error:
1855
1856 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
1857 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
1858
1859 return cli_dynerr(appctx, err);
1860}
1861
1862
1863
1864
1865/*
1866 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
1867 */
1868static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
1869{
1870 struct ckch_store *new_ckchs = NULL;
1871 struct ckch_store *old_ckchs = NULL;
1872 char *err = NULL;
1873 int i;
William Lallemandda8584c2020-05-14 10:14:37 +02001874 int errcode = 0;
1875 char *end;
1876 int type = CERT_TYPE_PEM;
1877 struct cert_key_and_chain *ckch;
1878 struct buffer *buf;
1879
1880 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1881 return 1;
1882
William Lallemandda8584c2020-05-14 10:14:37 +02001883 if (!*args[3] || !payload)
1884 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
1885
1886 /* The operations on the CKCH architecture are locked so we can
1887 * manipulate ckch_store and ckch_inst */
1888 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
1889 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
1890
William Lallemand5ba80d62021-05-04 16:17:27 +02001891 if ((buf = alloc_trash_chunk()) == NULL) {
1892 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1893 errcode |= ERR_ALERT | ERR_FATAL;
1894 goto end;
1895 }
William Lallemande5ff4ad2020-06-08 09:40:37 +02001896
William Lallemandda8584c2020-05-14 10:14:37 +02001897 if (!chunk_strcpy(buf, args[3])) {
1898 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1899 errcode |= ERR_ALERT | ERR_FATAL;
1900 goto end;
1901 }
1902
1903 /* check which type of file we want to update */
1904 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
1905 end = strrchr(buf->area, '.');
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001906 if (end && *cert_exts[i].ext && (strcmp(end + 1, cert_exts[i].ext) == 0)) {
William Lallemandda8584c2020-05-14 10:14:37 +02001907 *end = '\0';
William Lallemand089c1382020-10-23 17:35:12 +02001908 buf->data = strlen(buf->area);
William Lallemandda8584c2020-05-14 10:14:37 +02001909 type = cert_exts[i].type;
1910 break;
1911 }
1912 }
1913
1914 appctx->ctx.ssl.old_ckchs = NULL;
1915 appctx->ctx.ssl.new_ckchs = NULL;
1916
1917 /* if there is an ongoing transaction */
1918 if (ckchs_transaction.path) {
William Lallemandda8584c2020-05-14 10:14:37 +02001919 /* if there is an ongoing transaction, check if this is the same file */
1920 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
William Lallemand089c1382020-10-23 17:35:12 +02001921 /* we didn't find the transaction, must try more cases below */
1922
1923 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
1924 if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
1925 if (!chunk_strcat(buf, ".crt")) {
1926 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1927 errcode |= ERR_ALERT | ERR_FATAL;
1928 goto end;
1929 }
1930
1931 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
1932 /* remove .crt of the error message */
1933 *(b_orig(buf) + b_data(buf) + strlen(".crt")) = '\0';
1934 b_sub(buf, strlen(".crt"));
1935
1936 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
1937 errcode |= ERR_ALERT | ERR_FATAL;
1938 goto end;
1939 }
1940 }
William Lallemandda8584c2020-05-14 10:14:37 +02001941 }
1942
1943 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
1944
1945 } else {
William Lallemandda8584c2020-05-14 10:14:37 +02001946
William Lallemand95fefa12020-09-09 12:01:33 +02001947 /* lookup for the certificate in the tree */
1948 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
William Lallemand089c1382020-10-23 17:35:12 +02001949
1950 if (!appctx->ctx.ssl.old_ckchs) {
1951 /* if the del-ext option is activated we should try to take a look at a ".crt" too. */
1952 if (type != CERT_TYPE_PEM && global_ssl.extra_files_noext) {
1953 if (!chunk_strcat(buf, ".crt")) {
1954 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1955 errcode |= ERR_ALERT | ERR_FATAL;
1956 goto end;
1957 }
1958 appctx->ctx.ssl.old_ckchs = ckchs_lookup(buf->area);
1959 }
1960 }
William Lallemandda8584c2020-05-14 10:14:37 +02001961 }
1962
1963 if (!appctx->ctx.ssl.old_ckchs) {
1964 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
1965 err ? err : "");
1966 errcode |= ERR_ALERT | ERR_FATAL;
1967 goto end;
1968 }
1969
1970 if (!appctx->ctx.ssl.path) {
1971 /* this is a new transaction, set the path of the transaction */
1972 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
1973 if (!appctx->ctx.ssl.path) {
1974 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
1975 errcode |= ERR_ALERT | ERR_FATAL;
1976 goto end;
1977 }
1978 }
1979
1980 old_ckchs = appctx->ctx.ssl.old_ckchs;
1981
1982 /* duplicate the ckch store */
1983 new_ckchs = ckchs_dup(old_ckchs);
1984 if (!new_ckchs) {
1985 memprintf(&err, "%sCannot allocate memory!\n",
1986 err ? err : "");
1987 errcode |= ERR_ALERT | ERR_FATAL;
1988 goto end;
1989 }
1990
William Lallemand95fefa12020-09-09 12:01:33 +02001991 ckch = new_ckchs->ckch;
William Lallemandda8584c2020-05-14 10:14:37 +02001992
1993 /* appply the change on the duplicate */
1994 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
1995 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
1996 errcode |= ERR_ALERT | ERR_FATAL;
1997 goto end;
1998 }
1999
2000 appctx->ctx.ssl.new_ckchs = new_ckchs;
2001
2002 /* we succeed, we can save the ckchs in the transaction */
2003
2004 /* if there wasn't a transaction, update the old ckchs */
2005 if (!ckchs_transaction.old_ckchs) {
2006 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
2007 ckchs_transaction.path = appctx->ctx.ssl.path;
2008 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
2009 } else {
2010 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
2011
2012 }
2013
2014 /* free the previous ckchs if there was a transaction */
2015 ckch_store_free(ckchs_transaction.new_ckchs);
2016
2017 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
2018
2019
2020 /* creates the SNI ctxs later in the IO handler */
2021
2022end:
2023 free_trash_chunk(buf);
2024
2025 if (errcode & ERR_CODE) {
2026
2027 ckch_store_free(appctx->ctx.ssl.new_ckchs);
2028 appctx->ctx.ssl.new_ckchs = NULL;
2029
2030 appctx->ctx.ssl.old_ckchs = NULL;
2031
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002032 ha_free(&appctx->ctx.ssl.path);
William Lallemandda8584c2020-05-14 10:14:37 +02002033
2034 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2035 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2036 } else {
2037
2038 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2039 return cli_dynmsg(appctx, LOG_NOTICE, err);
2040 }
2041 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
2042}
2043
2044/* parsing function of 'abort ssl cert' */
2045static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
2046{
2047 char *err = NULL;
2048
2049 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2050 return 1;
2051
2052 if (!*args[3])
2053 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
2054
2055 /* The operations on the CKCH architecture are locked so we can
2056 * manipulate ckch_store and ckch_inst */
2057 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2058 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2059
2060 if (!ckchs_transaction.path) {
2061 memprintf(&err, "No ongoing transaction!\n");
2062 goto error;
2063 }
2064
2065 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
2066 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
2067 goto error;
2068 }
2069
2070 /* Only free the ckchs there, because the SNI and instances were not generated yet */
2071 ckch_store_free(ckchs_transaction.new_ckchs);
2072 ckchs_transaction.new_ckchs = NULL;
William Lallemandda8584c2020-05-14 10:14:37 +02002073 ckchs_transaction.old_ckchs = NULL;
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002074 ha_free(&ckchs_transaction.path);
William Lallemandda8584c2020-05-14 10:14:37 +02002075
2076 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2077
2078 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2079 return cli_dynmsg(appctx, LOG_NOTICE, err);
2080
2081error:
2082 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2083
2084 return cli_dynerr(appctx, err);
2085}
2086
2087/* parsing function of 'new ssl cert' */
2088static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
2089{
2090 struct ckch_store *store;
2091 char *err = NULL;
2092 char *path;
2093
2094 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2095 return 1;
2096
2097 if (!*args[3])
2098 return cli_err(appctx, "'new ssl cert' expects a filename\n");
2099
2100 path = args[3];
2101
2102 /* The operations on the CKCH architecture are locked so we can
2103 * manipulate ckch_store and ckch_inst */
2104 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2105 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
2106
2107 store = ckchs_lookup(path);
2108 if (store != NULL) {
2109 memprintf(&err, "Certificate '%s' already exists!\n", path);
2110 store = NULL; /* we don't want to free it */
2111 goto error;
2112 }
2113 /* we won't support multi-certificate bundle here */
William Lallemandbd8e6ed2020-09-16 16:08:08 +02002114 store = ckch_store_new(path);
William Lallemandda8584c2020-05-14 10:14:37 +02002115 if (!store) {
2116 memprintf(&err, "unable to allocate memory.\n");
2117 goto error;
2118 }
2119
2120 /* insert into the ckchs tree */
2121 ebst_insert(&ckchs_tree, &store->node);
2122 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
2123
2124 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2125 return cli_dynmsg(appctx, LOG_NOTICE, err);
2126error:
2127 free(store);
2128 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2129 return cli_dynerr(appctx, err);
2130}
2131
2132/* parsing function of 'del ssl cert' */
2133static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
2134{
2135 struct ckch_store *store;
2136 char *err = NULL;
2137 char *filename;
2138
2139 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2140 return 1;
2141
2142 if (!*args[3])
2143 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
2144
2145 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2146 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
2147
2148 filename = args[3];
2149
2150 store = ckchs_lookup(filename);
2151 if (store == NULL) {
2152 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
2153 goto error;
2154 }
2155 if (!LIST_ISEMPTY(&store->ckch_inst)) {
2156 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
2157 goto error;
2158 }
2159
2160 ebmb_delete(&store->node);
2161 ckch_store_free(store);
2162
2163 memprintf(&err, "Certificate '%s' deleted!\n", filename);
2164
2165 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2166 return cli_dynmsg(appctx, LOG_NOTICE, err);
2167
2168error:
2169 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
2170 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2171 return cli_dynerr(appctx, err);
2172}
2173
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002174
2175/*
2176 * Parsing function of `set ssl ca-file`
2177 */
2178static int cli_parse_set_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2179{
2180 char *err = NULL;
2181 int errcode = 0;
2182 struct buffer *buf;
2183
2184 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2185 return 1;
2186
2187 if (!*args[3] || !payload)
2188 return cli_err(appctx, "'set ssl ca-file expects a filename and CAs as a payload\n");
2189
2190 /* The operations on the CKCH architecture are locked so we can
2191 * manipulate ckch_store and ckch_inst */
2192 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2193 return cli_err(appctx, "Can't update the CA file!\nOperations on certificates are currently locked!\n");
2194
2195 if ((buf = alloc_trash_chunk()) == NULL) {
2196 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2197 errcode |= ERR_ALERT | ERR_FATAL;
2198 goto end;
2199 }
2200
2201 if (!chunk_strcpy(buf, args[3])) {
2202 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2203 errcode |= ERR_ALERT | ERR_FATAL;
2204 goto end;
2205 }
2206
2207 appctx->ctx.ssl.old_cafile_entry = NULL;
2208 appctx->ctx.ssl.new_cafile_entry = NULL;
2209
2210 /* if there is an ongoing transaction */
2211 if (cafile_transaction.path) {
2212 /* if there is an ongoing transaction, check if this is the same file */
2213 if (strcmp(cafile_transaction.path, buf->area) != 0) {
2214 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, buf->area);
2215 errcode |= ERR_ALERT | ERR_FATAL;
2216 goto end;
2217 }
2218 appctx->ctx.ssl.old_cafile_entry = cafile_transaction.old_cafile_entry;
2219 }
2220 else {
2221 /* lookup for the certificate in the tree */
2222 appctx->ctx.ssl.old_cafile_entry = ssl_store_get_cafile_entry(buf->area, 0);
2223 }
2224
2225 if (!appctx->ctx.ssl.old_cafile_entry) {
2226 memprintf(&err, "%sCan't replace a CA file which is not referenced by the configuration!\n",
2227 err ? err : "");
2228 errcode |= ERR_ALERT | ERR_FATAL;
2229 goto end;
2230 }
2231
2232 if (!appctx->ctx.ssl.path) {
2233 /* this is a new transaction, set the path of the transaction */
2234 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_cafile_entry->path);
2235 if (!appctx->ctx.ssl.path) {
2236 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
2237 errcode |= ERR_ALERT | ERR_FATAL;
2238 goto end;
2239 }
2240 }
2241
2242 if (appctx->ctx.ssl.new_cafile_entry)
2243 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_cafile_entry);
2244
2245 /* Create a new cafile_entry without adding it to the cafile tree. */
Remi Tricot-Le Breton0bb48242021-04-16 17:59:23 +02002246 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 +01002247 if (!appctx->ctx.ssl.new_cafile_entry) {
2248 memprintf(&err, "%sCannot allocate memory!\n",
2249 err ? err : "");
2250 errcode |= ERR_ALERT | ERR_FATAL;
2251 goto end;
2252 }
2253
2254 /* Fill the new entry with the new CAs. */
2255 if (ssl_store_load_ca_from_buf(appctx->ctx.ssl.new_cafile_entry, payload)) {
2256 memprintf(&err, "%sInvalid payload\n", err ? err : "");
2257 errcode |= ERR_ALERT | ERR_FATAL;
2258 goto end;
2259 }
2260
2261 /* we succeed, we can save the ca in the transaction */
2262
2263 /* if there wasn't a transaction, update the old CA */
2264 if (!cafile_transaction.old_cafile_entry) {
2265 cafile_transaction.old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2266 cafile_transaction.path = appctx->ctx.ssl.path;
2267 err = memprintf(&err, "transaction created for CA %s!\n", cafile_transaction.path);
2268 } else {
2269 err = memprintf(&err, "transaction updated for CA %s!\n", cafile_transaction.path);
2270 }
2271
2272 /* free the previous CA if there was a transaction */
2273 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2274
2275 cafile_transaction.new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2276
2277 /* creates the SNI ctxs later in the IO handler */
2278
2279end:
2280 free_trash_chunk(buf);
2281
2282 if (errcode & ERR_CODE) {
2283 ssl_store_delete_cafile_entry(appctx->ctx.ssl.new_cafile_entry);
2284 appctx->ctx.ssl.new_cafile_entry = NULL;
2285 appctx->ctx.ssl.old_cafile_entry = NULL;
2286
2287 free(appctx->ctx.ssl.path);
2288 appctx->ctx.ssl.path = NULL;
2289
2290 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2291 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
2292 } else {
2293
2294 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2295 return cli_dynmsg(appctx, LOG_NOTICE, err);
2296 }
2297}
2298
2299
2300/*
2301 * Parsing function of 'commit ssl ca-file'
2302 */
2303static int cli_parse_commit_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2304{
2305 char *err = NULL;
2306
2307 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2308 return 1;
2309
2310 if (!*args[3])
2311 return cli_err(appctx, "'commit ssl ca-file expects a filename\n");
2312
2313 /* The operations on the CKCH architecture are locked so we can
2314 * manipulate ckch_store and ckch_inst */
2315 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2316 return cli_err(appctx, "Can't commit the CA file!\nOperations on certificates are currently locked!\n");
2317
2318 if (!cafile_transaction.path) {
2319 memprintf(&err, "No ongoing transaction! !\n");
2320 goto error;
2321 }
2322
2323 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2324 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", cafile_transaction.path, args[3]);
2325 goto error;
2326 }
2327 /* init the appctx structure */
2328 appctx->st2 = SETCERT_ST_INIT;
2329 appctx->ctx.ssl.next_ckchi_link = NULL;
2330 appctx->ctx.ssl.old_cafile_entry = cafile_transaction.old_cafile_entry;
2331 appctx->ctx.ssl.new_cafile_entry = cafile_transaction.new_cafile_entry;
2332
2333 return 0;
2334
2335error:
2336
2337 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2338 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
2339
2340 return cli_dynerr(appctx, err);
2341}
2342
2343enum {
2344 CREATE_NEW_INST_OK = 0,
2345 CREATE_NEW_INST_YIELD = -1,
2346 CREATE_NEW_INST_ERR = -2
2347};
2348
2349static inline int __create_new_instance(struct appctx *appctx, struct ckch_inst *ckchi, int *count,
2350 struct buffer *trash, char *err)
2351{
2352 struct ckch_inst *new_inst;
2353
2354 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
2355 if (*count >= 10) {
2356 /* save the next ckchi to compute */
2357 appctx->ctx.ssl.next_ckchi = ckchi;
2358 return CREATE_NEW_INST_YIELD;
2359 }
2360
2361 /* Rebuild a new ckch instance that uses the same ckch_store
2362 * than a reference ckchi instance but will use a new CA file. */
2363 if (ckch_inst_rebuild(ckchi->ckch_store, ckchi, &new_inst, &err))
2364 return CREATE_NEW_INST_ERR;
2365
2366 /* display one dot per new instance */
2367 chunk_appendf(trash, ".");
2368 ++(*count);
2369
2370 return CREATE_NEW_INST_OK;
2371}
2372
2373/*
2374 * This function tries to create new ckch instances and their SNIs using a newly
2375 * set certificate authority (CA file)
2376 */
2377static int cli_io_handler_commit_cafile(struct appctx *appctx)
2378{
2379 struct stream_interface *si = appctx->owner;
2380 int y = 0;
2381 char *err = NULL;
2382 int errcode = 0;
2383 struct cafile_entry *old_cafile_entry, *new_cafile_entry;
2384 struct ckch_inst_link *ckchi_link;
2385 struct buffer *trash = alloc_trash_chunk();
2386
2387 if (trash == NULL)
2388 goto error;
2389
2390 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2391 goto error;
2392
2393 while (1) {
2394 switch (appctx->st2) {
2395 case SETCERT_ST_INIT:
2396 /* This state just print the update message */
2397 chunk_printf(trash, "Committing %s", cafile_transaction.path);
2398 if (ci_putchk(si_ic(si), trash) == -1) {
2399 si_rx_room_blk(si);
2400 goto yield;
2401 }
2402 appctx->st2 = SETCERT_ST_GEN;
2403 /* fallthrough */
2404 case SETCERT_ST_GEN:
2405 /*
2406 * This state generates the ckch instances with their
2407 * sni_ctxs and SSL_CTX.
2408 *
2409 * Since the SSL_CTX generation can be CPU consumer, we
2410 * yield every 10 instances.
2411 */
2412 old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2413 new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2414
2415 if (!new_cafile_entry)
2416 continue;
2417
2418 /* get the next ckchi to regenerate */
2419 ckchi_link = appctx->ctx.ssl.next_ckchi_link;
2420 /* we didn't start yet, set it to the first elem */
2421 if (ckchi_link == NULL) {
2422 ckchi_link = LIST_ELEM(old_cafile_entry->ckch_inst_link.n, typeof(ckchi_link), list);
2423 /* Add the newly created cafile_entry to the tree so that
2424 * any new ckch instance created from now can use it. */
2425 if (ssl_store_add_uncommitted_cafile_entry(new_cafile_entry))
2426 goto error;
2427 }
2428
2429 list_for_each_entry_from(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
2430 switch (__create_new_instance(appctx, ckchi_link->ckch_inst, &y, trash, err)) {
2431 case CREATE_NEW_INST_YIELD:
2432 appctx->ctx.ssl.next_ckchi_link = ckchi_link;
2433 goto yield;
2434 case CREATE_NEW_INST_ERR:
2435 goto error;
2436 default: break;
2437 }
2438 }
2439
2440 appctx->st2 = SETCERT_ST_INSERT;
2441 /* fallthrough */
2442 case SETCERT_ST_INSERT:
2443 /* The generation is finished, we can insert everything */
2444
2445 old_cafile_entry = appctx->ctx.ssl.old_cafile_entry;
2446 new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2447
2448 if (!new_cafile_entry)
2449 continue;
2450
2451 /* insert the new ckch_insts in the crtlist_entry */
2452 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2453 if (ckchi_link->ckch_inst->crtlist_entry)
2454 LIST_INSERT(&ckchi_link->ckch_inst->crtlist_entry->ckch_inst,
2455 &ckchi_link->ckch_inst->by_crtlist_entry);
2456 }
2457
2458 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
2459 list_for_each_entry(ckchi_link, &new_cafile_entry->ckch_inst_link, list) {
2460 __ssl_sock_load_new_ckch_instance(ckchi_link->ckch_inst);
2461 }
2462
2463 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
2464 list_for_each_entry(ckchi_link, &old_cafile_entry->ckch_inst_link, list) {
2465 __ckch_inst_free_locked(ckchi_link->ckch_inst);
2466 }
2467
2468
2469 /* Remove the old cafile entry from the tree */
2470 ebmb_delete(&old_cafile_entry->node);
2471 ssl_store_delete_cafile_entry(old_cafile_entry);
2472
2473 appctx->st2 = SETCERT_ST_FIN;
2474 /* fallthrough */
2475 case SETCERT_ST_FIN:
2476 /* we achieved the transaction, we can set everything to NULL */
2477 ha_free(&cafile_transaction.path);
2478 cafile_transaction.old_cafile_entry = NULL;
2479 cafile_transaction.new_cafile_entry = NULL;
2480 goto end;
2481 }
2482 }
2483end:
2484
2485 chunk_appendf(trash, "\n");
2486 if (errcode & ERR_WARN)
2487 chunk_appendf(trash, "%s", err);
2488 chunk_appendf(trash, "Success!\n");
2489 if (ci_putchk(si_ic(si), trash) == -1)
2490 si_rx_room_blk(si);
2491 free_trash_chunk(trash);
2492 /* success: call the release function and don't come back */
2493 return 1;
2494yield:
2495 /* store the state */
2496 if (ci_putchk(si_ic(si), trash) == -1)
2497 si_rx_room_blk(si);
2498 free_trash_chunk(trash);
2499 si_rx_endp_more(si); /* let's come back later */
2500 return 0; /* should come back */
2501
2502error:
2503 /* spin unlock and free are done in the release function */
2504 if (trash) {
2505 chunk_appendf(trash, "\n%sFailed!\n", err);
2506 if (ci_putchk(si_ic(si), trash) == -1)
2507 si_rx_room_blk(si);
2508 free_trash_chunk(trash);
2509 }
2510 /* error: call the release function and don't come back */
2511 return 1;
2512}
2513
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002514
2515/* parsing function of 'abort ssl ca-file' */
2516static int cli_parse_abort_cafile(char **args, char *payload, struct appctx *appctx, void *private)
2517{
2518 char *err = NULL;
2519
2520 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2521 return 1;
2522
2523 if (!*args[3])
2524 return cli_err(appctx, "'abort ssl ca-file' expects a filename\n");
2525
2526 /* The operations on the CKCH architecture are locked so we can
2527 * manipulate ckch_store and ckch_inst */
2528 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
2529 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
2530
2531 if (!cafile_transaction.path) {
2532 memprintf(&err, "No ongoing transaction!\n");
2533 goto error;
2534 }
2535
2536 if (strcmp(cafile_transaction.path, args[3]) != 0) {
2537 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", cafile_transaction.path, args[3]);
2538 goto error;
2539 }
2540
2541 /* Only free the uncommitted cafile_entry here, because the SNI and instances were not generated yet */
2542 ssl_store_delete_cafile_entry(cafile_transaction.new_cafile_entry);
2543 cafile_transaction.new_cafile_entry = NULL;
2544 cafile_transaction.old_cafile_entry = NULL;
2545 ha_free(&cafile_transaction.path);
2546
2547 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2548
2549 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
2550 return cli_dynmsg(appctx, LOG_NOTICE, err);
2551
2552error:
2553 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2554
2555 return cli_dynerr(appctx, err);
2556}
2557
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002558/* release function of the `commit ssl ca-file' command, free things and unlock the spinlock */
2559static void cli_release_commit_cafile(struct appctx *appctx)
2560{
2561 if (appctx->st2 != SETCERT_ST_FIN) {
2562 struct cafile_entry *new_cafile_entry = appctx->ctx.ssl.new_cafile_entry;
2563
2564 /* Remove the uncommitted cafile_entry from the tree. */
2565 ebmb_delete(&new_cafile_entry->node);
2566 ssl_store_delete_cafile_entry(new_cafile_entry);
2567 }
2568 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
2569}
2570
2571
William Lallemandee8530c2020-06-23 18:19:42 +02002572void ckch_deinit()
2573{
2574 struct eb_node *node, *next;
2575 struct ckch_store *store;
2576
2577 node = eb_first(&ckchs_tree);
2578 while (node) {
2579 next = eb_next(node);
2580 store = ebmb_entry(node, struct ckch_store, node);
2581 ckch_store_free(store);
2582 node = next;
2583 }
2584}
William Lallemandda8584c2020-05-14 10:14:37 +02002585
2586/* register cli keywords */
2587static struct cli_kw_list cli_kws = {{ },{
Remi Tricot-Le Bretona32a68b2021-02-24 17:35:43 +01002588 { { "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 },
2589 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
2590 { { "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 },
2591 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
2592 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
2593 { { "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 },
2594
2595 { { "set", "ssl", "ca-file", NULL }, "set ssl ca-file <cafile> <payload> : replace a CA file", cli_parse_set_cafile, NULL, NULL },
2596 { { "commit", "ssl", "ca-file", NULL }, "commit ssl ca-file <cafile> : commit a CA file", cli_parse_commit_cafile, cli_io_handler_commit_cafile, cli_release_commit_cafile },
Remi Tricot-Le Bretond5fd09d2021-03-11 10:22:52 +01002597 { { "abort", "ssl", "ca-file", NULL }, "abort ssl ca-file <cafile> : abort a transaction for a CA file", cli_parse_abort_cafile, NULL, NULL },
William Lallemandda8584c2020-05-14 10:14:37 +02002598 { { NULL }, NULL, NULL, NULL }
2599}};
2600
2601INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
2602