]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/crypto/serv_crypto.c
I got a little too eager in removing old cruft from the TLS code. Not setting the...
[citadel.git] / citadel / modules / crypto / serv_crypto.c
index 6e81a7176de75e1873f95482805e1cd053fc3e9f..ee03fdaef91357e815791b9ffb43ef1962ab29e6 100644 (file)
@@ -42,7 +42,7 @@
 #ifdef HAVE_OPENSSL
 
 SSL_CTX *ssl_ctx = NULL;               // This SSL context is used for all sessions.
-
+char *ssl_cipher_list = CIT_CIPHERS;
 
 // If a private key does not exist, generate one now.
 void generate_key(char *keyfilename) {
@@ -213,17 +213,16 @@ void generate_certificate(char *keyfilename, char *certfilename) {
 // This is called during initialization, and can be called again later if the certificate changes.
 void bind_to_key_and_certificate(void) {
 
-       SSL_CTX *old_ctx, *new_ctx;
+       SSL_CTX *old_ctx = NULL;
+       SSL_CTX *new_ctx = NULL;
 
-       if (!(new_ctx = SSL_CTX_new(SSLv23_server_method()))) {
+       if (!(new_ctx = SSL_CTX_new(TLS_server_method()))) {
                syslog(LOG_ERR, "crypto: SSL_CTX_new failed: %s", ERR_reason_error_string(ERR_get_error()));
                return;
        }
 
-       if (!(SSL_CTX_set_cipher_list(new_ctx, CIT_CIPHERS))) {
-               syslog(LOG_ERR, "crypto: No ciphers available");
-               SSL_CTX_free(new_ctx);
-               new_ctx = NULL;
+       if (!(SSL_CTX_set_cipher_list(new_ctx, ssl_cipher_list))) {
+               syslog(LOG_ERR, "crypto: SSL_CTX_set_cipher_list failed: %s", ERR_reason_error_string(ERR_get_error()));
                return;
        }
 
@@ -232,15 +231,14 @@ void bind_to_key_and_certificate(void) {
 
        syslog(LOG_DEBUG, "crypto: using private key %s", file_crpt_file_key);
         SSL_CTX_use_PrivateKey_file(new_ctx, file_crpt_file_key, SSL_FILETYPE_PEM);
-        if ( !SSL_CTX_check_private_key(new_ctx) ) {
-               syslog(LOG_ERR, "crypto: cannot install certificate: %s", ERR_reason_error_string(ERR_get_error()));
-        }
 
        old_ctx = ssl_ctx;
        ssl_ctx = new_ctx;              // All future binds will use the new certificate
 
-       sleep(1);                       // Hopefully wait until all in-progress binds to the old certificate have completed
-       SSL_CTX_free(old_ctx);
+       if (old_ctx != NULL) {
+               sleep(1);               // Hopefully wait until all in-progress binds to the old certificate have completed
+               SSL_CTX_free(old_ctx);
+       }
 }
 
 
@@ -260,25 +258,32 @@ void update_key_and_cert_if_needed(void) {
        }
 
        if ((keystat.st_mtime + certstat.st_mtime) != previous_mtime) {
+               begin_critical_section(S_OPENSSL);
                bind_to_key_and_certificate();
+               end_critical_section(S_OPENSSL);
                previous_mtime = keystat.st_mtime + certstat.st_mtime;
        }
 }
 
 
-// Initialize the SSL/TLS subsystem.
+// Initialize the TLS subsystem.
 void init_ssl(void) {
-       SSL_library_init();                                             // Initialize SSL transport layer
+
+       // Initialize the OpenSSL library
        SSL_load_error_strings();
+       ERR_load_crypto_strings();
+       OpenSSL_add_all_algorithms();
+       SSL_library_init();
 
+       // Load (or generate) a key and certificate
        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
        generate_certificate(file_crpt_file_key, file_crpt_file_cer);   // If a certificate does not exist, create it
        bind_to_key_and_certificate();                                  // Load key and cert from disk, and bind to them.
 
-       // 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");
+       // Register some Citadel protocol commands for dealing with encrypted sessions
+       CtdlRegisterProtoHook(cmd_stls, "STLS", "Start TLS session");
+       CtdlRegisterProtoHook(cmd_gtls, "GTLS", "Get TLS session status");
        CtdlRegisterSessionHook(endtls, EVT_STOP, PRIO_STOP + 10);
 }
 
@@ -303,8 +308,7 @@ void client_write_ssl(const char *buf, int nbytes) {
                        long errval;
 
                        errval = SSL_get_error(CC->ssl, retval);
-                       if (errval == SSL_ERROR_WANT_READ ||
-                           errval == SSL_ERROR_WANT_WRITE) {
+                       if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
                                sleep(1);
                                continue;
                        }
@@ -382,27 +386,24 @@ int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int
                }
                else {
                        int n = 0;
-                       if ((pch > ChrPtr(IOBuf)) && 
-                           (*(pch - 1) == '\r')) {
+                       if ((pch > ChrPtr(IOBuf)) && (*(pch - 1) == '\r')) {
                                n = 1;
                        }
-                       StrBufAppendBufPlain(Line, pos, 
-                                            (pch - pos - n), 0);
+                       StrBufAppendBufPlain(Line, pos, (pch - pos - n), 0);
 
                        if (StrLength(IOBuf) <= (pch - ChrPtr(IOBuf) + 1)) {
                                FlushStrBuf(IOBuf);
                                *Pos = NULL;
                        }
-                       else 
+                       else {
                                *Pos = pch + 1;
+                       }
                        return StrLength(Line);
                }
        }
 
        pLF = NULL;
-       while ((nSuccessLess < timeout) && 
-              (pLF == NULL) &&
-              (CC->ssl != NULL)) {
+       while ((nSuccessLess < timeout) && (pLF == NULL) && (CC->ssl != NULL)) {
 
                rlen = client_read_sslbuffer(IOBuf, timeout);
                if (rlen < 1) {
@@ -419,12 +420,12 @@ int client_readline_sslbuffer(StrBuf *Line, StrBuf *IOBuf, const char **Pos, int
                if (len > 0 && (*(pLF - 1) == '\r') )
                        len --;
                StrBufAppendBufPlain(Line, pos, len, 0);
-               if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf))
-               {
+               if (pLF + 1 >= ChrPtr(IOBuf) + StrLength(IOBuf)) {
                        FlushStrBuf(IOBuf);
                }
-               else 
+               else  {
                        *Pos = pLF + 1;
+               }
                return StrLength(Line);
        }
        return -1;
@@ -480,7 +481,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);
                        FlushStrBuf(CC->RecvBuf.Buf);
                }
                else {
@@ -493,7 +494,7 @@ int client_read_sslblob(StrBuf *Target, long bytes, int timeout) {
 }
 
 
-// CtdlStartTLS() starts SSL/TLS encryption for the current session.  It
+// CtdlStartTLS() starts 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) {
@@ -541,21 +542,13 @@ void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response)
                // 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_ERR, "crypto: SSL_accept failed: retval=%d, errval=%ld, err=%s",
-                       retval,
-                       errval,
-                       ERR_error_string(errval, error_string)
-               );
+               syslog(LOG_ERR, "crypto: SSL_accept failed: %s", ERR_reason_error_string(ERR_get_error()));
                SSL_free(CC->ssl);
                CC->ssl = NULL;
                return;
        }
        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)",
+       syslog(LOG_INFO, "crypto: 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
@@ -564,7 +557,7 @@ void CtdlStartTLS(char *ok_response, char *nosup_response, char *error_response)
 }
 
 
-// cmd_stls() starts SSL/TLS encryption for the current session
+// cmd_stls() starts TLS encryption for the current session
 void cmd_stls(char *params) {
        char ok_response[SIZ];
        char nosup_response[SIZ];
@@ -588,9 +581,7 @@ void cmd_gtls(char *params) {
                cprintf("%d Session is not encrypted.\n", ERROR);
                return;
        }
-       bits =
-           SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl),
-                               &alg_bits);
+       bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(CC->ssl), &alg_bits);
        cprintf("%d %s|%s|%d|%d\n", CIT_OK,
                SSL_CIPHER_get_version(SSL_get_current_cipher(CC->ssl)),
                SSL_CIPHER_get_name(SSL_get_current_cipher(CC->ssl)),
@@ -599,16 +590,13 @@ 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) {
        if (!CC->ssl) {
                CC->redirect_ssl = 0;
                return;
        }
 
-       syslog(LOG_INFO, "crypto: ending SSL/TLS");
+       syslog(LOG_INFO, "crypto: ending TLS on this session");
        SSL_shutdown(CC->ssl);
        SSL_free(CC->ssl);
        CC->ssl = NULL;