]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/crypto/serv_crypto.c
ssl_ctx = SSL_CTX_new(SSLv23_server_method()) instead of using a temporary variable...
[citadel.git] / citadel / modules / crypto / serv_crypto.c
index 83545f2fcfd79c55f80c0f1e218a3329eed5377f..cbb190cde857e987a76c0b3a87a22622aec3ec9b 100644 (file)
@@ -1,4 +1,4 @@
-// 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
-pthread_mutex_t **SSLCritters;         // Things needing locking
-
-
-static unsigned long id_callback(void) {
-       return (unsigned long) pthread_self();
-}
+SSL_CTX *ssl_ctx;                              // SSL context for all sessions
 
 
 void generate_key(char *keyfilename) {
@@ -57,7 +51,7 @@ 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;
        }
 
@@ -94,15 +88,51 @@ void generate_key(char *keyfilename) {
                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;
+       }
 }
 
 
 void init_ssl(void) {
-       const SSL_METHOD *ssl_method;
        RSA *rsa = NULL;
        X509_REQ *req = NULL;
        X509 *cer = NULL;
@@ -111,29 +141,10 @@ void init_ssl(void) {
        X509_NAME *name = NULL;
        FILE *fp;
 
-       SSLCritters = malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
-       if (!SSLCritters) {
-               syslog(LOG_ERR, "crypto: can't allocate memory!");
-               exit(CTDLEXIT_CRYPTO);
-       }
-       else {
-               int a;
-
-               for (a = 0; a < CRYPTO_num_locks(); a++) {
-                       SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
-                       if (!SSLCritters[a]) {
-                               syslog(LOG_ERR, "crypto: can't allocate memory!!");
-                               exit(CTDLEXIT_CRYPTO);
-                       }
-                       pthread_mutex_init(SSLCritters[a], NULL);
-               }
-       }
-
        // Initialize SSL transport layer
        SSL_library_init();
        SSL_load_error_strings();
-       ssl_method = SSLv23_server_method();
-       if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
+       if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) {
                syslog(LOG_ERR, "crypto: SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
                return;
        }
@@ -144,9 +155,6 @@ void init_ssl(void) {
                return;
        }
 
-       CRYPTO_set_locking_callback(ssl_lock);
-       CRYPTO_set_id_callback(id_callback);
-
        mkdir(ctdl_key_dir, 0700);              // If the keys directory does not exist, create it
        generate_key(file_crpt_file_key);       // If a private key does not exist, create it
 
@@ -167,7 +175,6 @@ void init_ssl(void) {
                }
 
                if (rsa) {
-
                        // Create a public key from the private key
                        if (pk=EVP_PKEY_new(), pk != NULL) {
                                EVP_PKEY_assign_RSA(pk, rsa);
@@ -205,10 +212,8 @@ void init_ssl(void) {
                                        X509_REQ_free(req);
                                }
                        }
-
                        RSA_free(rsa);
                }
-
                else {
                        syslog(LOG_ERR, "crypto: unable to read private key.");
                }
@@ -274,11 +279,7 @@ void init_ssl(void) {
        }
 
        // 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");
@@ -367,18 +368,15 @@ int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int
        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');
                
@@ -515,10 +513,15 @@ void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response)
        }
 
        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) {
@@ -530,10 +533,14 @@ void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response)
                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
@@ -552,7 +559,6 @@ void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response)
                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)),
@@ -614,14 +620,4 @@ void endtls(void) {
        CC->redirect_ssl = 0;
 }
 
-
-// ssl_lock() callback for OpenSSL mutex locks
-void ssl_lock(int mode, int n, const char *file, int line) {
-       if (mode & CRYPTO_LOCK) {
-               pthread_mutex_lock(SSLCritters[n]);
-       }
-       else {
-               pthread_mutex_unlock(SSLCritters[n]);
-       }
-}
 #endif // HAVE_OPENSSL