When checking to see whether we have to rebind a new key and/or
[citadel.git] / citadel / modules / crypto / serv_crypto.c
index cb12d318570517ed3f37221f207795dc77ac0dae..302be080e427ec3d00c5e1653daeba6aa89c6674 100644 (file)
@@ -1,14 +1,12 @@
-/*
- * Copyright (c) 1987-2015 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.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
+// 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.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
 
 #include <string.h>
 #include <unistd.h>
 #include <openssl/rand.h>
 #endif
 
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
+#include <time.h>
 
 #ifdef HAVE_PTHREAD_H
 #include <pthread.h>
 #include "sysdep_decls.h"
 #include "citadel.h"
 #include "config.h"
-
-
 #include "ctdl_module.h"
-/* TODO: should we use the standard module init stuff to start this? */
-/* TODO: should we register an event handler to call destruct_ssl? */
 
 #ifdef HAVE_OPENSSL
-SSL_CTX *ssl_ctx;              /* SSL context */
-pthread_mutex_t **SSLCritters; /* Things needing locking */
+SSL_CTX *ssl_ctx;                              // SSL context for all sessions
+
+
+void generate_key(char *keyfilename) {
+       int ret = 0;
+       RSA *rsa = NULL;
+       BIGNUM *bne = NULL;
+       int bits = 2048;
+       unsigned long e = RSA_F4;
+       FILE *fp;
+
+       if (access(keyfilename, R_OK) == 0) {   // we already have a key, so don't generate a new one
+               return;
+       }
 
-static unsigned long id_callback(void)
-{
-       return (unsigned long) pthread_self();
+       syslog(LOG_INFO, "crypto: generating RSA key pair");
+       // generate rsa key
+       bne = BN_new();
+       ret = BN_set_word(bne,e);
+       if (ret != 1) {
+               goto free_all;
+       }
+       rsa = RSA_new();
+       ret = RSA_generate_key_ex(rsa, bits, bne, NULL);
+       if (ret != 1) {
+               goto free_all;
+       }
+
+       // write the key file
+       fp = fopen(keyfilename, "w");
+       if (fp != NULL) {
+               chmod(keyfilename, 0600);
+               if (PEM_write_RSAPrivateKey(fp, // the file
+                                       rsa,    // the key
+                                       NULL,   // no enc
+                                       NULL,   // no passphrase
+                                       0,      // no passphrase
+                                       NULL,   // no callback
+                                       NULL    // no callback
+               ) != 1) {
+                       syslog(LOG_ERR, "crypto: cannot write key: %s", ERR_reason_error_string(ERR_get_error()));
+                       unlink(keyfilename);
+               }
+               fclose(fp);
+       }
+
+       // free the memory we used
+free_all:
+       RSA_free(rsa);
+       BN_free(bne);
 }
 
-void destruct_ssl(void)
-{
-       int a;
-       for (a = 0; a < CRYPTO_num_locks(); a++) 
-               free(SSLCritters[a]);
-       free (SSLCritters);
+
+// 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)
-{
+
+void init_ssl(void) {
        const SSL_METHOD *ssl_method;
-       DH *dh;
-       RSA *rsa=NULL;
+       RSA *rsa = NULL;
        X509_REQ *req = NULL;
        X509 *cer = NULL;
        EVP_PKEY *pk = NULL;
@@ -83,143 +142,34 @@ void init_ssl(void)
        X509_NAME *name = NULL;
        FILE *fp;
 
-       if (!access(EGD_POOL, F_OK))
-               RAND_egd(EGD_POOL);
-
-       if (!RAND_status()) {
-               syslog(LOG_CRIT,
-                       "PRNG not adequately seeded, won't do SSL/TLS\n");
-               return;
-       }
-       SSLCritters =
-           malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t *));
-       if (!SSLCritters) {
-               syslog(LOG_EMERG, "citserver: can't allocate memory!!\n");
-               /* Nothing's been initialized, just die */
-               exit(1);
-       } else {
-               int a;
-
-               for (a = 0; a < CRYPTO_num_locks(); a++) {
-                       SSLCritters[a] = malloc(sizeof(pthread_mutex_t));
-                       if (!SSLCritters[a]) {
-                               syslog(LOG_EMERG,
-                                       "citserver: can't allocate memory!!\n");
-                               /* Nothing's been initialized, just die */
-                               exit(1);
-                       }
-                       pthread_mutex_init(SSLCritters[a], NULL);
-               }
-       }
-
-       /*
-        * Initialize SSL transport layer
-        */
+       // Initialize SSL transport layer
        SSL_library_init();
        SSL_load_error_strings();
        ssl_method = SSLv23_server_method();
        if (!(ssl_ctx = SSL_CTX_new(ssl_method))) {
-               syslog(LOG_CRIT, "SSL_CTX_new failed: %s\n",
-                       ERR_reason_error_string(ERR_get_error()));
+               syslog(LOG_ERR, "crypto: SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
                return;
        }
        if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
-               syslog(LOG_CRIT, "SSL: No ciphers available\n");
+               syslog(LOG_ERR, "crypto: No ciphers available");
                SSL_CTX_free(ssl_ctx);
                ssl_ctx = NULL;
                return;
        }
-#if 0
-#if SSLEAY_VERSION_NUMBER >= 0x00906000L
-       SSL_CTX_set_mode(ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
-                        SSL_MODE_AUTO_RETRY);
-#endif
-#endif
 
-       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
 
-       /* Load DH parameters into the context */
-       dh = DH_new();
-       if (!dh) {
-               syslog(LOG_CRIT, "init_ssl() can't allocate a DH object: %s\n",
-                       ERR_reason_error_string(ERR_get_error()));
-               SSL_CTX_free(ssl_ctx);
-               ssl_ctx = NULL;
-               return;
-       }
-       if (!(BN_hex2bn(&(dh->p), DH_P))) {
-               syslog(LOG_CRIT, "init_ssl() can't assign DH_P: %s\n",
-                       ERR_reason_error_string(ERR_get_error()));
-               SSL_CTX_free(ssl_ctx);
-               ssl_ctx = NULL;
-               return;
-       }
-       if (!(BN_hex2bn(&(dh->g), DH_G))) {
-               syslog(LOG_CRIT, "init_ssl() can't assign DH_G: %s\n",
-                       ERR_reason_error_string(ERR_get_error()));
-               SSL_CTX_free(ssl_ctx);
-               ssl_ctx = NULL;
-               return;
-       }
-       dh->length = DH_L;
-       SSL_CTX_set_tmp_dh(ssl_ctx, dh);
-       DH_free(dh);
-
-       /* Get our certificates in order.
-        * First, create the key/cert directory if it's not there already...
-        */
-       mkdir(ctdl_key_dir, 0700);
-
-       /*
-        * Generate a key pair if we don't have one.
-        */
-       if (access(file_crpt_file_key, R_OK) != 0) {
-               syslog(LOG_INFO, "Generating RSA key pair.\n");
-               rsa = RSA_generate_key(1024,    /* modulus size */
-                                       65537,  /* exponent */
-                                       NULL,   /* no callback */
-                                       NULL);  /* no callback */
-               if (rsa == NULL) {
-                       syslog(LOG_CRIT, "Key generation failed: %s\n",
-                               ERR_reason_error_string(ERR_get_error()));
-               }
-               if (rsa != NULL) {
-                       fp = fopen(file_crpt_file_key, "w");
-                       if (fp != NULL) {
-                               chmod(file_crpt_file_key, 0600);
-                               if (PEM_write_RSAPrivateKey(fp, /* the file */
-                                                       rsa,    /* the key */
-                                                       NULL,   /* no enc */
-                                                       NULL,   /* no passphr */
-                                                       0,      /* no passphr */
-                                                       NULL,   /* no callbk */
-                                                       NULL    /* no callbk */
-                               ) != 1) {
-                                       syslog(LOG_CRIT, "Cannot write key: %s\n",
-                                               ERR_reason_error_string(ERR_get_error()));
-                                       unlink(file_crpt_file_key);
-                               }
-                               fclose(fp);
-                       }
-                       RSA_free(rsa);
-               }
-       }
-
-       /*
-        * If there is no certificate file on disk, we will be generating a self-signed certificate
-        * in the next step.  Therefore, if we have neither a CSR nor a certificate, generate
-        * the CSR in this step so that the next step may commence.
-        */
+       // If there is no certificate file on disk, we will be generating a self-signed certificate
+       // in the next step.  Therefore, if we have neither a CSR nor a certificate, generate
+       // the CSR in this step so that the next step may commence.
        if ( (access(file_crpt_file_cer, R_OK) != 0) && (access(file_crpt_file_csr, R_OK) != 0) ) {
-               syslog(LOG_INFO, "Generating a certificate signing request.\n");
-
-               /*
-                * Read our key from the file.  No, we don't just keep this
-                * in memory from the above key-generation function, because
-                * there is the possibility that the key was already on disk
-                * and we didn't just generate it now.
-                */
+               syslog(LOG_INFO, "crypto: generating a generic certificate signing request.");
+
+               // Read our key from the file.  No, we don't just keep this
+               // in memory from the above key-generation function, because
+               // there is the possibility that the key was already on disk
+               // and we didn't just generate it now.
                fp = fopen(file_crpt_file_key, "r");
                if (fp) {
                        rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
@@ -227,58 +177,32 @@ void init_ssl(void)
                }
 
                if (rsa) {
-
-                       /* Create a public key from the private key */
+                       // Create a public key from the private key
                        if (pk=EVP_PKEY_new(), pk != NULL) {
                                EVP_PKEY_assign_RSA(pk, rsa);
                                if (req = X509_REQ_new(), req != NULL) {
 
-                                       /* Set the public key */
+                                       // Set the public key
                                        X509_REQ_set_pubkey(req, pk);
                                        X509_REQ_set_version(req, 0L);
 
                                        name = X509_REQ_get_subject_name(req);
 
-                                       /* Tell it who we are */
-
-                                       /*
-                                       X509_NAME_add_entry_by_txt(name, "C",
-                                               MBSTRING_ASC, "US", -1, -1, 0);
-
-                                       X509_NAME_add_entry_by_txt(name, "ST",
-                                               MBSTRING_ASC, "New York", -1, -1, 0);
-
-                                       X509_NAME_add_entry_by_txt(name, "L",
-                                               MBSTRING_ASC, "Mount Kisco", -1, -1, 0);
-                                       */
-
-                                       X509_NAME_add_entry_by_txt(name, "O",
-                                                                  MBSTRING_ASC, 
-                                                                  (unsigned char*) CtdlGetConfigStr("c_humannode"),
-                                                                  -1, -1, 0);
-
-                                       X509_NAME_add_entry_by_txt(name, "OU",
-                                                                  MBSTRING_ASC, 
-                                                                  (unsigned const char*)"Citadel server",
-                                                                  -1, -1, 0);
-
-                                       /* X509_NAME_add_entry_by_txt(name, "CN",
-                                               MBSTRING_ASC, CtdlGetConfigStr("c_fqdn"), -1, -1, 0);
-                                       */
-
-                                       X509_NAME_add_entry_by_txt(name, 
-                                                                  "CN",
-                                                                  MBSTRING_ASC, 
-                                                                  (const unsigned char *)"*", -1, -1, 0);
-                               
+                                       // Tell it who we are
+                                       X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned const char *)"ZZ", -1, -1, 0);
+                                       X509_NAME_add_entry_by_txt(name, "ST", MBSTRING_ASC, (unsigned const char *)"The World", -1, -1, 0);
+                                       X509_NAME_add_entry_by_txt(name, "L", MBSTRING_ASC, (unsigned const char *)"My Location", -1, -1, 0);
+                                       X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned const char *)"Generic certificate", -1, -1, 0);
+                                       X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_ASC, (unsigned const char *)"Citadel server", -1, -1, 0);
+                                       X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned const char *)"*", -1, -1, 0);
                                        X509_REQ_set_subject_name(req, name);
 
-                                       /* Sign the CSR */
+                                       // Sign the CSR
                                        if (!X509_REQ_sign(req, pk, EVP_md5())) {
-                                               syslog(LOG_CRIT, "X509_REQ_sign(): error\n");
+                                               syslog(LOG_ERR, "crypto: X509_REQ_sign(): error");
                                        }
                                        else {
-                                               /* Write it to disk. */ 
+                                               // Write it to disk
                                                fp = fopen(file_crpt_file_csr, "w");
                                                if (fp != NULL) {
                                                        chmod(file_crpt_file_csr, 0600);
@@ -290,33 +214,26 @@ void init_ssl(void)
                                        X509_REQ_free(req);
                                }
                        }
-
                        RSA_free(rsa);
                }
-
                else {
-                       syslog(LOG_CRIT, "Unable to read private key.\n");
+                       syslog(LOG_ERR, "crypto: unable to read private key.");
                }
        }
 
-
-
-       /*
-        * Generate a self-signed certificate if we don't have one.
-        */
+       // Generate a self-signed certificate if we don't have one.
        if (access(file_crpt_file_cer, R_OK) != 0) {
-               syslog(LOG_INFO, "Generating a self-signed certificate.\n");
+               syslog(LOG_INFO, "crypto: generating a generic self-signed certificate.");
 
-               /* Same deal as before: always read the key from disk because
-                * it may or may not have just been generated.
-                */
+               // Same deal as before: always read the key from disk because
+               // it may or may not have just been generated.
                fp = fopen(file_crpt_file_key, "r");
                if (fp) {
                        rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
                        fclose(fp);
                }
 
-               /* This also holds true for the CSR. */
+               // This also holds true for the CSR.
                req = NULL;
                cer = NULL;
                pk = NULL;
@@ -333,22 +250,21 @@ void init_ssl(void)
 
                        if (req) {
                                if (cer = X509_new(), cer != NULL) {
-
                                        ASN1_INTEGER_set(X509_get_serialNumber(cer), 0);
-                                       X509_set_issuer_name(cer, req->req_info->subject);
-                                       X509_set_subject_name(cer, req->req_info->subject);
+                                       X509_set_issuer_name(cer, X509_REQ_get_subject_name(req));
+                                       X509_set_subject_name(cer, X509_REQ_get_subject_name(req));
                                        X509_gmtime_adj(X509_get_notBefore(cer),0);
                                        X509_gmtime_adj(X509_get_notAfter(cer),(long)60*60*24*SIGN_DAYS);
                                        req_pkey = X509_REQ_get_pubkey(req);
                                        X509_set_pubkey(cer, req_pkey);
                                        EVP_PKEY_free(req_pkey);
                                        
-                                       /* Sign the cert */
+                                       // Sign the cert
                                        if (!X509_sign(cer, pk, EVP_md5())) {
-                                               syslog(LOG_CRIT, "X509_sign(): error\n");
+                                               syslog(LOG_ERR, "crypto: X509_sign() error");
                                        }
                                        else {
-                                               /* Write it to disk. */ 
+                                               // Write it to disk.
                                                fp = fopen(file_crpt_file_cer, "w");
                                                if (fp != NULL) {
                                                        chmod(file_crpt_file_cer, 0600);
@@ -364,30 +280,18 @@ void init_ssl(void)
                }
        }
 
+       // Now try to bind to the key and certificate.
+       bind_to_key_and_certificate();
 
-       /*
-        * 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_CRIT, "Cannot install certificate: %s\n",
-                               ERR_reason_error_string(ERR_get_error()));
-        }
-
-       /* Finally let the server know we're here */
+       // Finally let the server know we're here
        CtdlRegisterProtoHook(cmd_stls, "STLS", "Start SSL/TLS session");
-       CtdlRegisterProtoHook(cmd_gtls, "GTLS",
-                             "Get SSL/TLS session status");
+       CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get SSL/TLS session status");
        CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
 }
 
 
-/*
- * client_write_ssl() Send binary data to the client encrypted.
- */
-void client_write_ssl(const char *buf, int nbytes)
-{
+// client_write_ssl() Send binary data to the client encrypted.
+void client_write_ssl(const char *buf, int nbytes) {
        int retval;
        int nremain;
        char junk[1];
@@ -397,7 +301,7 @@ void client_write_ssl(const char *buf, int nbytes)
        while (nremain > 0) {
                if (SSL_want_write(CC->ssl)) {
                        if ((SSL_read(CC->ssl, junk, 0)) < 1) {
-                               syslog(LOG_DEBUG, "SSL_read in client_write: %s\n", ERR_reason_error_string(ERR_get_error()));
+                               syslog(LOG_DEBUG, "crypto: SSL_read in client_write: %s", ERR_reason_error_string(ERR_get_error()));
                        }
                }
                retval =
@@ -411,9 +315,10 @@ void client_write_ssl(const char *buf, int nbytes)
                                sleep(1);
                                continue;
                        }
-                       syslog(LOG_DEBUG, "SSL_write got error %ld, ret %d\n", errval, retval);
-                       if (retval == -1)
-                               syslog(LOG_DEBUG, "errno is %d\n", errno);
+                       syslog(LOG_DEBUG, "crypto: SSL_write got error %ld, ret %d", errval, retval);
+                       if (retval == -1) {
+                               syslog(LOG_DEBUG, "crypto: errno is %d", errno);
+                       }
                        endtls();
                        client_write(&buf[nbytes - nremain], nremain);
                        return;
@@ -423,12 +328,9 @@ void client_write_ssl(const char *buf, int nbytes)
 }
 
 
-/*
- * read data from the encrypted layer.
- */
-int client_read_sslbuffer(StrBuf *buf, int timeout)
-{
-       char sbuf[16384]; /* OpenSSL communicates in 16k blocks, so let's speak its native tongue. */
+// read data from the encrypted layer.
+int client_read_sslbuffer(StrBuf *buf, int timeout) {
+       char sbuf[16384];       // OpenSSL communicates in 16k blocks, so let's speak its native tongue.
        int rlen;
        char junk[1];
        SSL *pssl = CC->ssl;
@@ -438,7 +340,7 @@ int client_read_sslbuffer(StrBuf *buf, int timeout)
        while (1) {
                if (SSL_want_read(pssl)) {
                        if ((SSL_write(pssl, junk, 0)) < 1) {
-                               syslog(LOG_DEBUG, "SSL_write in client_read\n");
+                               syslog(LOG_DEBUG, "crypto: SSL_write in client_read");
                        }
                }
                rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
@@ -450,7 +352,7 @@ int client_read_sslbuffer(StrBuf *buf, int timeout)
                                sleep(1);
                                continue;
                        }
-                       syslog(LOG_DEBUG, "SSL_read got error %ld\n", errval);
+                       syslog(LOG_DEBUG, "crypto: SSL_read got error %ld", errval);
                        endtls();
                        return (-1);
                }
@@ -460,36 +362,28 @@ int client_read_sslbuffer(StrBuf *buf, int timeout)
        return (0);
 }
 
-int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout)
-{
-       CitContext *CCC = CC;
+
+int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int timeout) {
        const char *pos = NULL;
        const char *pLF;
        int len, rlen;
        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;
-//             *Error = ErrRBLF_PreConditionFailed;
+               }
                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 (pch == NULL) {
-                       StrBufAppendBufPlain(Line, pos, 
-                                            StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
+                       StrBufAppendBufPlain(Line, pos, StrLength(IOBuf) - (pos - ChrPtr(IOBuf)), 0);
                        FlushStrBuf(IOBuf);
                        *Pos = NULL;
                }
@@ -515,13 +409,10 @@ int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int
        pLF = NULL;
        while ((nSuccessLess < timeout) && 
               (pLF == NULL) &&
-              (CCC->ssl != NULL)) {
+              (CC->ssl != NULL)) {
 
                rlen = client_read_sslbuffer(IOBuf, timeout);
                if (rlen < 1) {
-//                     *Error = strerror(errno);
-//                     close(*fd);
-//                     *fd = -1;
                        return -1;
                }
                else if (rlen > 0) {
@@ -543,74 +434,64 @@ int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int
                        *Pos = pLF + 1;
                return StrLength(Line);
        }
-//     *Error = ErrRBLF_NotEnoughSentFromServer;
        return -1;
 }
 
 
-
-int client_read_sslblob(StrBuf *Target, long bytes, int timeout)
-{
+int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
        long baselen;
        long RemainRead;
        int retval = 0;
-       CitContext *CCC = CC;
 
        baselen = StrLength(Target);
 
-       if (StrLength(CCC->RecvBuf.Buf) > 0)
-       {
+       if (StrLength(CC->RecvBuf.Buf) > 0) {
                long RemainLen;
                long TotalLen;
                const char *pchs;
 
-               if (CCC->RecvBuf.ReadWritePointer == NULL)
-                       CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf);
-               pchs = ChrPtr(CCC->RecvBuf.Buf);
-               TotalLen = StrLength(CCC->RecvBuf.Buf);
-               RemainLen = TotalLen - (pchs - CCC->RecvBuf.ReadWritePointer);
-               if (RemainLen > bytes)
+               if (CC->RecvBuf.ReadWritePointer == NULL) {
+                       CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf);
+               }
+               pchs = ChrPtr(CC->RecvBuf.Buf);
+               TotalLen = StrLength(CC->RecvBuf.Buf);
+               RemainLen = TotalLen - (pchs - CC->RecvBuf.ReadWritePointer);
+               if (RemainLen > bytes) {
                        RemainLen = bytes;
-               if (RemainLen > 0)
-               {
-                       StrBufAppendBufPlain(Target, 
-                                            CCC->RecvBuf.ReadWritePointer, 
-                                            RemainLen, 0);
-                       CCC->RecvBuf.ReadWritePointer += RemainLen;
                }
-               if ((ChrPtr(CCC->RecvBuf.Buf) + StrLength(CCC->RecvBuf.Buf)) <= CCC->RecvBuf.ReadWritePointer)
-               {
-                       CCC->RecvBuf.ReadWritePointer = NULL;
-                       FlushStrBuf(CCC->RecvBuf.Buf);
+               if (RemainLen > 0) {
+                       StrBufAppendBufPlain(Target, CC->RecvBuf.ReadWritePointer, RemainLen, 0);
+                       CC->RecvBuf.ReadWritePointer += RemainLen;
+               }
+               if ((ChrPtr(CC->RecvBuf.Buf) + StrLength(CC->RecvBuf.Buf)) <= CC->RecvBuf.ReadWritePointer) {
+                       CC->RecvBuf.ReadWritePointer = NULL;
+                       FlushStrBuf(CC->RecvBuf.Buf);
                }
        }
 
-       if (StrLength(Target) >= bytes + baselen)
+       if (StrLength(Target) >= bytes + baselen) {
                return 1;
+       }
 
-       CCC->RecvBuf.ReadWritePointer = NULL;
+       CC->RecvBuf.ReadWritePointer = NULL;
 
-       while ((StrLength(Target) < bytes + baselen) &&
-              (retval >= 0))
-       {
-               retval = client_read_sslbuffer(CCC->RecvBuf.Buf, timeout);
+       while ((StrLength(Target) < bytes + baselen) && (retval >= 0)) {
+               retval = client_read_sslbuffer(CC->RecvBuf.Buf, timeout);
                if (retval >= 0) {
                        RemainRead = bytes - (StrLength (Target) - baselen);
-                       if (RemainRead < StrLength(CCC->RecvBuf.Buf))
-                       {
+                       if (RemainRead < StrLength(CC->RecvBuf.Buf)) {
                                StrBufAppendBufPlain(
                                        Target, 
-                                       ChrPtr(CCC->RecvBuf.Buf), 
+                                       ChrPtr(CC->RecvBuf.Buf), 
                                        RemainRead, 0);
-                               CCC->RecvBuf.ReadWritePointer = ChrPtr(CCC->RecvBuf.Buf) + RemainRead;
+                               CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf) + RemainRead;
                                break;
                        }
-                       StrBufAppendBuf(Target, CCC->RecvBuf.Buf, 0); /* todo: Buf > bytes? */
-                       FlushStrBuf(CCC->RecvBuf.Buf);
+                       StrBufAppendBuf(Target, CC->RecvBuf.Buf, 0);    // todo: Buf > bytes?
+                       FlushStrBuf(CC->RecvBuf.Buf);
                }
-               else 
-               {
-                       FlushStrBuf(CCC->RecvBuf.Buf);
+               else {
+                       FlushStrBuf(CC->RecvBuf.Buf);
                        return -1;
        
                }
@@ -619,48 +500,59 @@ int client_read_sslblob(StrBuf *Target, long bytes, int timeout)
 }
 
 
-/*
- * CtdlStartTLS() starts SSL/TLS encryption for the current session.  It
- * must be supplied with pre-generated strings for responses of "ok," "no
- * support for TLS," and "error" so that we can use this in any protocol.
- */
-void CtdlStartTLS(char *ok_response, char *nosup_response,
-                       char *error_response) {
-
+// CtdlStartTLS() starts SSL/TLS encryption for the current session.  It
+// must be supplied with pre-generated strings for responses of "ok," "no
+// support for TLS," and "error" so that we can use this in any protocol.
+void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response) {
        int retval, bits, alg_bits;
 
+       if (CC->redirect_ssl) {
+               syslog(LOG_ERR, "crypto: attempt to begin SSL on an already encrypted connection");
+               if (error_response != NULL) {
+                       cprintf("%s", error_response);
+               }
+               return;
+       }
+
        if (!ssl_ctx) {
-               syslog(LOG_CRIT, "SSL failed: no ssl_ctx exists?\n");
-               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_CRIT, "SSL_new failed: %s\n",
-                               ERR_reason_error_string(ERR_get_error()));
-               if (error_response != NULL) cprintf("%s", error_response);
+               syslog(LOG_ERR, "crypto: SSL_new failed: %s", ERR_reason_error_string(ERR_get_error()));
+               if (error_response != NULL) {
+                       cprintf("%s", error_response);
+               }
                return;
        }
        if (!(SSL_set_fd(CC->ssl, CC->client_socket))) {
-               syslog(LOG_CRIT, "SSL_set_fd failed: %s\n",
-                       ERR_reason_error_string(ERR_get_error()));
+               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
-                * discover the problem at the SSL layer and should
-                * revert to unencrypted communications.
-                */
+               // Can't notify the client of an error here; they will
+               // discover the problem at the SSL layer and should
+               // revert to unencrypted communications.
                long errval;
                char error_string[128];
 
                errval = SSL_get_error(CC->ssl, retval);
-               syslog(LOG_CRIT, "SSL_accept failed: retval=%d, errval=%ld, err=%s\n",
+               syslog(LOG_ERR, "crypto: SSL_accept failed: retval=%d, errval=%ld, err=%s",
                        retval,
                        errval,
                        ERR_error_string(errval, error_string)
@@ -669,46 +561,34 @@ void CtdlStartTLS(char *ok_response, char *nosup_response,
                CC->ssl = NULL;
                return;
        }
-       BIO_set_close(CC->ssl->rbio, BIO_NOCLOSE);
        bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
-       syslog(LOG_INFO, "SSL/TLS using %s on %s (%d of %d bits)\n",
+       syslog(LOG_INFO, "crypto: SSL/TLS using %s on %s (%d of %d bits)",
                SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
                SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
-               bits, alg_bits);
+               bits, alg_bits
+       );
        CC->redirect_ssl = 1;
 }
 
 
-/*
- * cmd_stls() starts SSL/TLS encryption for the current session
- */
-void cmd_stls(char *params)
-{
+// cmd_stls() starts SSL/TLS encryption for the current session
+void cmd_stls(char *params) {
        char ok_response[SIZ];
        char nosup_response[SIZ];
        char error_response[SIZ];
 
        unbuffer_output();
 
-       sprintf(ok_response,
-               "%d Begin TLS negotiation now\n",
-               CIT_OK);
-       sprintf(nosup_response,
-               "%d TLS not supported here\n",
-               ERROR + CMD_NOT_SUPPORTED);
-       sprintf(error_response,
-               "%d TLS negotiation error\n",
-               ERROR + INTERNAL_ERROR);
+       sprintf(ok_response, "%d Begin TLS negotiation now\n", CIT_OK);
+       sprintf(nosup_response, "%d TLS not supported here\n", ERROR + CMD_NOT_SUPPORTED);
+       sprintf(error_response, "%d TLS negotiation error\n", ERROR + INTERNAL_ERROR);
 
        CtdlStartTLS(ok_response, nosup_response, error_response);
 }
 
 
-/*
- * cmd_gtls() returns status info about the TLS connection
- */
-void cmd_gtls(char *params)
-{
+// cmd_gtls() returns status info about the TLS connection
+void cmd_gtls(char *params) {
        int bits, alg_bits;
 
        if (!CC->ssl || !CC->redirect_ssl) {
@@ -725,35 +605,21 @@ void cmd_gtls(char *params)
 }
 
 
-/*
- * endtls() shuts down the TLS connection
- *
- * WARNING:  This may make your session vulnerable to a known plaintext
- * attack in the current implmentation.
- */
-void endtls(void)
-{
+// endtls() shuts down the TLS connection
+//
+// WARNING:  This may make your session vulnerable to a known plaintext
+// attack in the current implmentation.
+void endtls(void) {
        if (!CC->ssl) {
                CC->redirect_ssl = 0;
                return;
        }
 
-       syslog(LOG_INFO, "Ending SSL/TLS\n");
+       syslog(LOG_INFO, "crypto: ending SSL/TLS");
        SSL_shutdown(CC->ssl);
        SSL_free(CC->ssl);
        CC->ssl = NULL;
        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 */
+#endif // HAVE_OPENSSL