]> code.citadel.org Git - citadel.git/blobdiff - webcit/crypto.c
* sanitize crypto reading
[citadel.git] / webcit / crypto.c
index 1515ff39762e4a195f00d1435947dd4151d00acf..fda0c406d605ad65eea88aa1711914059d8eefaa 100644 (file)
@@ -14,7 +14,7 @@
 #include "webserver.h"
 /** \todo dirify */
 /** where to find the keys */
-#define        CTDL_CRYPTO_DIR         "./keys" 
+#define        CTDL_CRYPTO_DIR         ctdl_key_dir
 #define CTDL_KEY_PATH          file_crpt_file_key /**< the key */
 #define CTDL_CSR_PATH          file_crpt_file_csr /**< the csr file */
 #define CTDL_CER_PATH          file_crpt_file_cer /**< the cer file */
@@ -34,6 +34,19 @@ static unsigned long id_callback(void)
        return (unsigned long) pthread_self();
 }
 
+void shutdown_ssl(void)
+{
+       ERR_free_strings();
+
+       /* Openssl requires these while shutdown. 
+        * Didn't find a way to get out of this clean.
+        * int i, n = CRYPTO_num_locks();
+        * for (i = 0; i < n; i++)
+        *      free(SSLCritters[i]);
+        * free(SSLCritters);
+       */
+}
+
 /**
  * \brief initialize ssl engine
  * load certs and initialize openssl internals
@@ -63,6 +76,7 @@ void init_ssl(void)
        if (!SSLCritters) {
                lprintf(1, "citserver: can't allocate memory!!\n");
                /* Nothing's been initialized, just die */
+               ShutDownWebcit();
                exit(WC_EXIT_SSL);
        } else {
                int a;
@@ -73,6 +87,7 @@ void init_ssl(void)
                                lprintf(1,
                                        "citserver: can't allocate memory!!\n");
                                /** Nothing's been initialized, just die */
+                               ShutDownWebcit();
                                exit(WC_EXIT_SSL);
                        }
                        pthread_mutex_init(SSLCritters[a], NULL);
@@ -148,14 +163,21 @@ void init_ssl(void)
                                }
                                fclose(fp);
                        }
+                       else {
+                               lprintf(3, "Cannot write key: %s\n", CTDL_KEY_PATH);
+                               ShutDownWebcit();
+                               exit(0);
+                       }
                        RSA_free(rsa);
                }
        }
 
-       /**
-        * Generate a CSR if we don't have one.
+       /*
+        * 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(CTDL_CSR_PATH, R_OK) != 0) {
+       if ( (access(CTDL_CER_PATH, R_OK) != 0) && (access(CTDL_CSR_PATH, R_OK) != 0) ) {
                lprintf(5, "Generating a certificate signing request.\n");
 
                /**
@@ -196,14 +218,22 @@ void init_ssl(void)
                                                MBSTRING_ASC, "Mount Kisco", -1, -1, 0);
                                        */
 
-                                       X509_NAME_add_entry_by_txt(name, "O",
-                                               MBSTRING_ASC, "Organization name", -1, -1, 0);
-
-                                       X509_NAME_add_entry_by_txt(name, "OU",
-                                               MBSTRING_ASC, "Citadel server", -1, -1, 0);
-
-                                       X509_NAME_add_entry_by_txt(name, "CN",
-                                               MBSTRING_ASC, "*", -1, -1, 0);
+                                       X509_NAME_add_entry_by_txt(
+                                               name, "O",
+                                               MBSTRING_ASC, 
+                                               (unsigned char*)"Organization name",
+                                               -1, -1, 0);
+
+                                       X509_NAME_add_entry_by_txt(
+                                               name, "OU",
+                                               MBSTRING_ASC, 
+                                               (unsigned char*)"Citadel server1",
+                                               -1, -1, 0);
+
+                                       X509_NAME_add_entry_by_txt(
+                                               name, "CN",
+                                               MBSTRING_ASC, 
+                                               (unsigned char*)"*", -1, -1, 0);
                                
                                        X509_REQ_set_subject_name(req, name);
 
@@ -219,6 +249,11 @@ void init_ssl(void)
                                                        PEM_write_X509_REQ(fp, req);
                                                        fclose(fp);
                                                }
+                                               else {
+                                                       lprintf(3, "Cannot write key: %s\n", CTDL_CSR_PATH);
+                                                       ShutDownWebcit();
+                                                       exit(0);
+                                               }
                                        }
 
                                        X509_REQ_free(req);
@@ -290,6 +325,11 @@ void init_ssl(void)
                                                        PEM_write_X509(fp, cer);
                                                        fclose(fp);
                                                }
+                                               else {
+                                                       lprintf(3, "Cannot write key: %s\n", CTDL_CER_PATH);
+                                                       ShutDownWebcit();
+                                                       exit(0);
+                                               }
                                        }
                                        X509_free(cer);
                                }
@@ -320,7 +360,7 @@ void init_ssl(void)
  * \return Zero if the SSL/TLS handshake succeeded, non-zero otherwise.
  */
 int starttls(int sock) {
-       int retval, bits, alg_bits;
+       int retval, bits, alg_bits, r;
        SSL *newssl;
 
        pthread_setspecific(ThreadSSL, NULL);
@@ -347,22 +387,40 @@ int starttls(int sock) {
                 * revert to unencrypted communications.
                 */
                long errval;
+               const char *ssl_error_reason = NULL;
 
                errval = SSL_get_error(newssl, retval);
-               lprintf(3, "SSL_accept failed: %s\n",
-                       ERR_reason_error_string(ERR_get_error()));
+               ssl_error_reason = ERR_reason_error_string(ERR_get_error());
+               if (ssl_error_reason == NULL)
+                       lprintf(3, "SSL_accept failed: errval=%i, retval=%i\n", errval, retval);
+               else
+                       lprintf(3, "SSL_accept failed: %s\n", ssl_error_reason);
+               sleeeeeeeeeep(1);
+               retval = SSL_accept(newssl);
+       }
+       if (retval < 1) {
+               long errval;
+               const char *ssl_error_reason = NULL;
+
+               errval = SSL_get_error(newssl, retval);
+               ssl_error_reason = ERR_reason_error_string(ERR_get_error());
+               if (ssl_error_reason == NULL)
+                       lprintf(3, "SSL_accept failed: errval=%i, retval=%i\n", errval, retval);
+               else
+                       lprintf(3, "SSL_accept failed: %s\n", ssl_error_reason);
                SSL_free(newssl);
                newssl = NULL;
                return(4);
-       }
-       BIO_set_close(newssl->rbio, BIO_NOCLOSE);
+       } else lprintf(15, "SSL_accept success\n");
+       r = BIO_set_close(newssl->rbio, BIO_NOCLOSE);
        bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(newssl), &alg_bits);
-       lprintf(5, "SSL/TLS using %s on %s (%d of %d bits)\n",
+       lprintf(15, "SSL/TLS using %s on %s (%d of %d bits)\n",
                SSL_CIPHER_get_name(SSL_get_current_cipher(newssl)),
                SSL_CIPHER_get_version(SSL_get_current_cipher(newssl)),
                bits, alg_bits);
 
        pthread_setspecific(ThreadSSL, newssl);
+       lprintf(15, "SSL started\n");
        return(0);
 }
 
@@ -380,7 +438,7 @@ void endtls(void)
 
        if (THREADSSL == NULL) return;
 
-       lprintf(5, "Ending SSL/TLS\n");
+       lprintf(15, "Ending SSL/TLS\n");
        SSL_shutdown(THREADSSL);
        ctx = SSL_get_SSL_CTX(THREADSSL);
 
@@ -417,15 +475,18 @@ void ssl_lock(int mode, int n, const char *file, int line)
  * \param buf chars to send to the client
  * \param nbytes how many chars
  */
-void client_write_ssl(char *buf, int nbytes)
+void client_write_ssl(const StrBuf *Buf)
 {
+       const char *buf;
        int retval;
        int nremain;
+       long nbytes;
        char junk[1];
 
        if (THREADSSL == NULL) return;
 
-       nremain = nbytes;
+       nbytes = nremain = StrLength(Buf);
+       buf = ChrPtr(Buf);
 
        while (nremain > 0) {
                if (SSL_want_write(THREADSSL)) {
@@ -441,7 +502,7 @@ void client_write_ssl(char *buf, int nbytes)
                        errval = SSL_get_error(THREADSSL, retval);
                        if (errval == SSL_ERROR_WANT_READ ||
                            errval == SSL_ERROR_WANT_WRITE) {
-                               sleep(1);
+                               sleeeeeeeeeep(1);
                                continue;
                        }
                        lprintf(9, "SSL_write got error %ld, ret %d\n", errval, retval);
@@ -463,7 +524,7 @@ void client_write_ssl(char *buf, int nbytes)
  * \param timeout how long should we wait?
  * \returns what???
  */
-int client_read_ssl(char *buf, int bytes, int timeout)
+int client_read_sslbuffer(StrBuf *buf, int timeout)
 {
 #if 0
        fd_set rfds;
@@ -471,20 +532,21 @@ int client_read_ssl(char *buf, int bytes, int timeout)
        int retval;
        int s;
 #endif
-       int len, rlen;
+       char sbuf[16384]; /**< Openssl communicates in 16k blocks, so lets speak its native tongue. */
+       int rlen;
        char junk[1];
+       SSL *pssl = THREADSSL;
 
-       if (THREADSSL == NULL) return(0);
+       if (pssl == NULL) return(-1);
 
-       len = 0;
-       while (len < bytes) {
+       while (1) {
 #if 0
                /**
                 * This code is disabled because we don't need it when
                 * using blocking reads (which we are). -IO
                 */
                FD_ZERO(&rfds);
-               s = BIO_get_fd(THREADSSL->rbio, NULL);
+               s = BIO_get_fd(pssl->rbio, NULL);
                FD_SET(s, &rfds);
                tv.tv_sec = timeout;
                tv.tv_usec = 0;
@@ -496,28 +558,29 @@ int client_read_ssl(char *buf, int bytes, int timeout)
                }
 
 #endif
-               if (SSL_want_read(THREADSSL)) {
-                       if ((SSL_write(THREADSSL, junk, 0)) < 1) {
-                               lprintf(9, "SSL_write in client_read: %s\n", ERR_reason_error_string(ERR_get_error()));
+               if (SSL_want_read(pssl)) {
+                       if ((SSL_write(pssl, junk, 0)) < 1) {
+                               lprintf(9, "SSL_write in client_read\n");
                        }
                }
-               rlen = SSL_read(THREADSSL, &buf[len], bytes - len);
+               rlen = SSL_read(pssl, sbuf, sizeof(sbuf));
                if (rlen < 1) {
                        long errval;
 
-                       errval = SSL_get_error(THREADSSL, rlen);
+                       errval = SSL_get_error(pssl, rlen);
                        if (errval == SSL_ERROR_WANT_READ ||
                            errval == SSL_ERROR_WANT_WRITE) {
-                               sleep(1);
+                               sleeeeeeeeeep(1);
                                continue;
                        }
                        lprintf(9, "SSL_read got error %ld\n", errval);
                        endtls();
-                       return (0);
+                       return (-1);
                }
-               len += rlen;
+               StrBufAppendBufPlain(buf, sbuf, rlen, 0);
+               return rlen;
        }
-       return (1);
+       return (0);
 }