-// Copyright (c) 1987-2021 by the citadel.org team
+// Copyright (c) 1987-2022 by the citadel.org team
//
// This program is open source software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3.
#include "ctdl_module.h"
#ifdef HAVE_OPENSSL
-SSL_CTX *ssl_ctx; // SSL context
+SSL_CTX *ssl_ctx; // SSL context for all sessions
void generate_key(char *keyfilename) {
unsigned long e = RSA_F4;
FILE *fp;
- if (access(keyfilename, R_OK) == 0) {
+ if (access(keyfilename, R_OK) == 0) { // we already have a key, so don't generate a new one
return;
}
fclose(fp);
}
- // free the memory we used
+ // free the memory we used
free_all:
- RSA_free(rsa);
- BN_free(bne);
+ RSA_free(rsa);
+ BN_free(bne);
+}
+
+
+// Set the private key and certificate chain for the global SSL Context.
+// This is called during initialization, and can be called again later if the certificate changes.
+void bind_to_key_and_certificate(void) {
+
+ syslog(LOG_DEBUG, "crypto: using certificate chain %s", file_crpt_file_cer);
+ SSL_CTX_use_certificate_chain_file(ssl_ctx, file_crpt_file_cer);
+
+ syslog(LOG_DEBUG, "crypto: using private key %s", file_crpt_file_key);
+ SSL_CTX_use_PrivateKey_file(ssl_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
+ if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
+ syslog(LOG_ERR, "crypto: cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
+ }
+}
+
+
+// Check the modification time of the key and certificate -- reload if they changed
+void update_key_and_cert_if_needed(void) {
+ static time_t previous_mtime = 0;
+ struct stat keystat;
+ struct stat certstat;
+
+ if (stat(file_crpt_file_key, &keystat) != 0) {
+ syslog(LOG_ERR, "%s: %s", file_crpt_file_key, strerror(errno));
+ return;
+ }
+ if (stat(file_crpt_file_cer, &certstat) != 0) {
+ syslog(LOG_ERR, "%s: %s", file_crpt_file_cer, strerror(errno));
+ return;
+ }
+
+ if ((keystat.st_mtime + certstat.st_mtime) != previous_mtime) {
+ bind_to_key_and_certificate();
+ previous_mtime = keystat.st_mtime + certstat.st_mtime;
+ }
}
}
if (rsa) {
-
// Create a public key from the private key
if (pk=EVP_PKEY_new(), pk != NULL) {
EVP_PKEY_assign_RSA(pk, rsa);
X509_REQ_free(req);
}
}
-
RSA_free(rsa);
}
-
else {
syslog(LOG_ERR, "crypto: unable to read private key.");
}
}
// Now try to bind to the key and certificate.
- SSL_CTX_use_certificate_chain_file(ssl_ctx, file_crpt_file_cer);
- SSL_CTX_use_PrivateKey_file(ssl_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
- if ( !SSL_CTX_check_private_key(ssl_ctx) ) {
- syslog(LOG_ERR, "crypto: cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
- }
+ bind_to_key_and_certificate();
// Finally let the server know we're here
CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
int nSuccessLess = 0;
const char *pch = NULL;
- if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL))
- {
- if (Pos != NULL)
- {
+ if ((Line == NULL) || (Pos == NULL) || (IOBuf == NULL)) {
+ if (Pos != NULL) {
*Pos = NULL;
}
return -1;
}
pos = *Pos;
- if ((StrLength(IOBuf) > 0) && (pos != NULL) && (pos < ChrPtr(IOBuf) + StrLength(IOBuf)))
- {
+ if ((StrLength(IOBuf) > 0) && (pos != NULL) && (pos < ChrPtr(IOBuf) + StrLength(IOBuf))) {
pch = pos;
pch = strchr(pch, '\n');
}
if (!ssl_ctx) {
- syslog(LOG_ERR, "crypto: SSL failed: no ssl_ctx exists?");
- if (nosup_response != NULL) cprintf("%s", nosup_response);
+ syslog(LOG_ERR, "crypto: SSL failed: context has not been initialized");
+ if (nosup_response != NULL) {
+ cprintf("%s", nosup_response);
+ }
return;
}
+
+ update_key_and_cert_if_needed(); // did someone update the key or cert? if so, re-bind them
+
if (!(CC->ssl = SSL_new(ssl_ctx))) {
syslog(LOG_ERR, "crypto: SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
if (error_response != NULL) {
syslog(LOG_ERR, "crypto: SSL_set_fd failed: %s", ERR_reason_error_string(ERR_get_error()));
SSL_free(CC->ssl);
CC->ssl = NULL;
- if (error_response != NULL) cprintf("%s", error_response);
+ if (error_response != NULL) {
+ cprintf("%s", error_response);
+ }
return;
}
- if (ok_response != NULL) cprintf("%s", ok_response);
+ if (ok_response != NULL) {
+ cprintf("%s", ok_response);
+ }
retval = SSL_accept(CC->ssl);
if (retval < 1) {
// Can't notify the client of an error here; they will
CC->ssl = NULL;
return;
}
- // BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE); not needed anymore in openssl 1.1 ?
bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
syslog(LOG_INFO, "crypto: SSL/TLS using %s on %s (%d of %d bits)",
SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),