]> 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 c5e312a0242307f0adb195988fbae577473d638b..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 "sysdep_decls.h"
 #include "citadel.h"
 #include "config.h"
-
-
 #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();
-}
-
-
-void destruct_ssl(void) {
-       int a;
-       for (a = 0; a < CRYPTO_num_locks(); a++) 
-               free(SSLCritters[a]);
-       free (SSLCritters);
-}
+SSL_CTX *ssl_ctx;                              // SSL context for all sessions
 
 
 void generate_key(char *keyfilename) {
@@ -67,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;
        }
 
@@ -90,13 +74,13 @@ void generate_key(char *keyfilename) {
        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 passphr */
-                                       0,      /* no passphr */
-                                       NULL,   /* no callbk */
-                                       NULL    /* no callbk */
+               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);
@@ -104,15 +88,51 @@ void generate_key(char *keyfilename) {
                fclose(fp);
        }
 
-    // 4. free
+       // 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;
@@ -121,31 +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
-        */
+       // 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;
        }
@@ -156,26 +155,19 @@ 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
 
-       /*
-        * 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, "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.
-                */
+               // 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);
@@ -183,19 +175,18 @@ 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 */
+                                       // 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);
@@ -204,12 +195,12 @@ void init_ssl(void) {
                                        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_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);
@@ -221,32 +212,26 @@ void init_ssl(void) {
                                        X509_REQ_free(req);
                                }
                        }
-
                        RSA_free(rsa);
                }
-
                else {
                        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, "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;
@@ -272,12 +257,12 @@ void init_ssl(void) {
                                        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_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);
@@ -293,17 +278,10 @@ 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_ERR, "crypto: cannot install certificate: %s", 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");
        CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
@@ -350,7 +328,7 @@ 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. */
+       char sbuf[16384];       // OpenSSL communicates in 16k blocks, so let's speak its native tongue.
        int rlen;
        char junk[1];
        SSL *pssl = CC->ssl;
@@ -390,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');
                
@@ -468,30 +443,25 @@ int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
 
        baselen = StrLength(Target);
 
-       if (StrLength(CC->RecvBuf.Buf) > 0)
-       {
+       if (StrLength(CC->RecvBuf.Buf) > 0) {
                long RemainLen;
                long TotalLen;
                const char *pchs;
 
-               if (CC->RecvBuf.ReadWritePointer == NULL)
-               {
+               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)
-               {
+               if (RemainLen > bytes) {
                        RemainLen = bytes;
                }
-               if (RemainLen > 0)
-               {
+               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)
-               {
+               if ((ChrPtr(CC->RecvBuf.Buf) + StrLength(CC->RecvBuf.Buf)) <= CC->RecvBuf.ReadWritePointer) {
                        CC->RecvBuf.ReadWritePointer = NULL;
                        FlushStrBuf(CC->RecvBuf.Buf);
                }
@@ -507,8 +477,7 @@ int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
                retval = client_read_sslbuffer(CC->RecvBuf.Buf, timeout);
                if (retval >= 0) {
                        RemainRead = bytes - (StrLength (Target) - baselen);
-                       if (RemainRead < StrLength(CC->RecvBuf.Buf))
-                       {
+                       if (RemainRead < StrLength(CC->RecvBuf.Buf)) {
                                StrBufAppendBufPlain(
                                        Target, 
                                        ChrPtr(CC->RecvBuf.Buf), 
@@ -516,7 +485,7 @@ int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
                                CC->RecvBuf.ReadWritePointer = ChrPtr(CC->RecvBuf.Buf) + RemainRead;
                                break;
                        }
-                       StrBufAppendBuf(Target, CC->RecvBuf.Buf, 0); /* todo: Buf > bytes? */
+                       StrBufAppendBuf(Target, CC->RecvBuf.Buf, 0);    // todo: Buf > bytes?
                        FlushStrBuf(CC->RecvBuf.Buf);
                }
                else {
@@ -544,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) {
@@ -559,17 +533,19 @@ 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
-                * 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];
 
@@ -583,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)),
@@ -645,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 */
+#endif // HAVE_OPENSSL