]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blobdiff - src/connection.c
Update copyright year.
[tlsproxy/tlsproxy.git] / src / connection.c
index c91eae358e6a3b7cfa44376afba42c928e96ff6b..184ee5a4231a302ee520dfecb05b845cdde0e4bb 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Handle connections.
  *
- * Copyright (C) 2011-2013  Simon Ruderich
+ * Copyright (C) 2011-2014  Simon Ruderich
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include <limits.h>
 #include <netdb.h>
 #include <poll.h>
+#include <sys/socket.h>
 #include <unistd.h>
 
+#include <gnutls/x509.h>
+
 
 /* Maximum length of a HTTP request line. Longer request lines are aborted
  * with an error. The standard doesn't specify a maximum line length but this
@@ -38,7 +41,8 @@
 /* Format string used to send HTTP/1.0 error responses to the client.
  *
  * %s is used 5 times, first is the error code, then additional headers, next
- * two are the error code (no %n$s!), the last is the message. */
+ * two are the error code (no %n$s which is not in C98!), the last is the
+ * message. */
 #define HTTP_RESPONSE_FORMAT "HTTP/1.0 %s\r\n\
 Content-Type: text/html; charset=US-ASCII\r\n\
 %s\r\n\
@@ -84,6 +88,8 @@ static int connect_to_host(const char *hostname, const char *port);
 static int parse_request(const char *buffer, char *host, char *port,
                                              int *version_minor);
 
+static void log_session_information(gnutls_session_t session);
+
 
 void handle_connection(int client_socket) {
     int server_socket;
@@ -138,7 +144,7 @@ void handle_connection(int client_socket) {
         send_bad_request(client_fd_write);
         goto out;
     } else if (result == -3) {
-        LOG(DEBUG1, "read_http_request(): proxy authentication failed");
+        LOG(WARNING, "read_http_request(): proxy authentication failed");
         send_authentication_required(client_fd_write);
         goto out;
     }
@@ -149,7 +155,7 @@ void handle_connection(int client_socket) {
         goto out;
     }
 
-    LOG(DEBUG1, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
+    LOG(DEBUG2, "target: %s:%s (HTTP 1.%d)", host, port, version_minor);
 
     /* Connect to proxy server or directly to server. */
     if (global_proxy_host != NULL && global_proxy_port != NULL) {
@@ -192,7 +198,7 @@ void handle_connection(int client_socket) {
 
         /* Check response of proxy server. */
         if (strncmp(buffer, "HTTP/1.0 200", 12)) {
-            LOG(WARNING, "bad proxy response: %s", buffer);
+            LOG(WARNING, "bad proxy response: >%s<", buffer);
             send_forwarding_failure(client_fd_write);
             goto out;
         }
@@ -201,7 +207,7 @@ void handle_connection(int client_socket) {
     LOG(DEBUG1, "connection to server established");
 
     /* If the -u option is used and we don't know this hostname's server
-     * certificate then just pass through the connection and let the client
+     * certificate, then just pass through the connection and let the client
      * verify the server certificate. */
     if (global_passthrough_unknown) {
         char path[TLSPROXY_MAX_PATH_LENGTH];
@@ -253,6 +259,10 @@ void handle_connection(int client_socket) {
 
     LOG(DEBUG1, "server TLS handshake finished");
 
+    if (global_log_level >= LOG_DEBUG2_LEVEL) {
+        log_session_information(server_session);
+    }
+
     /* Make sure the server certificate is valid and known. */
     if (verify_tls_connection(server_session, host) != 0) {
         LOG(ERROR, "server certificate validation failed!");
@@ -296,6 +306,10 @@ void handle_connection(int client_socket) {
 
     LOG(DEBUG1, "client TLS handshake finished");
 
+    if (global_log_level >= LOG_DEBUG2_LEVEL) {
+        log_session_information(client_session);
+    }
+
     /* Tell the client that the verification failed. Shouldn't be necessary as
      * the client should terminate the connection because he received the
      * invalid certificate but better be sure. */
@@ -337,8 +351,6 @@ out:
     }
     if (client_session_init) {
         gnutls_deinit(client_session);
-        gnutls_certificate_free_cas(client_x509_cred);
-        gnutls_certificate_free_keys(client_x509_cred);
         gnutls_certificate_free_credentials(client_x509_cred);
     }
 
@@ -606,6 +618,7 @@ static void tls_send_invalid_cert_message(gnutls_session_t session) {
     const char msg[]   = "Server certificate validation failed, check logs.";
 
     int result;
+    ssize_t size_written;
     char buffer[sizeof(HTTP_RESPONSE_FORMAT)
                 + 3 * sizeof(error) + sizeof(msg)];
 
@@ -613,7 +626,13 @@ static void tls_send_invalid_cert_message(gnutls_session_t session) {
                                               error, "", error, error, msg);
     assert(result > 0 && (size_t)result < sizeof(buffer));
 
-    gnutls_record_send(session, buffer, strlen(buffer));
+    size_written = gnutls_record_send(session, buffer, strlen(buffer));
+    if (size_written < 0) {
+        LOG(WARNING, "tls_send_invalid_cert_message(): "
+                     "gnutls_record_send(): %s",
+                     gnutls_strerror((int)size_written));
+    }
+    /* Just an error message, no need to check if everything was written. */
 }
 
 
@@ -628,7 +647,7 @@ static void transfer_data(int client, int server) {
     fds[1].events  = POLLIN | POLLPRI | POLLHUP | POLLERR;
     fds[1].revents = 0;
 
-    LOG(DEBUG1, "transfer_data(): %d -> %d", client, server);
+    LOG(DEBUG2, "transfer_data(): %d -> %d", client, server);
 
     for (;;) {
         int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
@@ -685,8 +704,8 @@ static int read_from_write_to(int from, int to) {
         return -1;
     }
     if (size_read != size_written) {
-        LOG(ERROR, "read_from_write_to(): only written %ld of %ld bytes!",
-                   (long int)size_written, (long int)size_read);
+        LOG(ERROR, "read_from_write_to(): only written %zu of %zu bytes!",
+                   size_written, size_read);
         return -1;
     }
 
@@ -713,8 +732,8 @@ static void transfer_data_tls(int client, int server,
     if (gnutls_record_get_max_size(server_session) < buffer_size) {
         buffer_size = gnutls_record_get_max_size(server_session);
     }
-    LOG(DEBUG1, "transfer_data_tls(): suggested buffer size: %ld",
-                (long int)buffer_size);
+    LOG(DEBUG2, "transfer_data_tls(): suggested buffer size: %zu",
+                buffer_size);
 
     for (;;) {
         int result = poll(fds, 2 /* fd count */, -1 /* no timeout */);
@@ -757,16 +776,32 @@ static int read_from_write_to_tls(gnutls_session_t from,
                                   size_t buffer_size) {
     ssize_t size_read;
     ssize_t size_written;
-    char buffer[16384];
+    char buffer[16384]; /* GnuTLS default maximum */
 
     if (buffer_size > sizeof(buffer)) {
-        LOG(WARNING, "read_from_write_to_tls(): reduced buffer size to %ld",
-                     (long int)(sizeof(buffer)));
+        LOG(WARNING, "read_from_write_to_tls(): reduced buffer size to %zu",
+                     sizeof(buffer));
         buffer_size = sizeof(buffer);
     }
 
     size_read = gnutls_record_recv(from, buffer, buffer_size);
     if (size_read < 0) {
+        /* Allow rehandshakes. As handshakes might be insecure make sure that
+         * %SAFE_RENEGOTIATION is used in GnuTLS's priority string. */
+        if (size_read == GNUTLS_E_REHANDSHAKE) {
+            int result;
+
+            LOG(DEBUG1, "server requested TLS rehandshake");
+
+            result = gnutls_handshake(from);
+            if (result != GNUTLS_E_SUCCESS) {
+                LOG(WARNING, "server TLS rehandshake failed: %s",
+                             gnutls_strerror(result));
+                return -1;
+            }
+            return 0;
+        }
+
         LOG(WARNING, "read_from_write_to_tls(): gnutls_record_recv(): %s",
                      gnutls_strerror((int)size_read));
         return -1;
@@ -782,8 +817,8 @@ static int read_from_write_to_tls(gnutls_session_t from,
         return -1;
     }
     if (size_read != size_written) {
-        LOG(ERROR, "read_from_write_to_tls(): only written %ld of %ld bytes!",
-                   (long int)size_written, (long int)size_read);
+        LOG(ERROR, "read_from_write_to_tls(): only written %zu of %zu bytes!",
+                   size_written, size_read);
         return -1;
     }
 
@@ -809,8 +844,10 @@ static int connect_to_host(const char *hostname, const char *port) {
     gai_hints.ai_socktype = SOCK_STREAM;
     gai_hints.ai_protocol = 0;
     gai_hints.ai_flags    = AI_NUMERICSERV /* given port is numeric */
+#ifdef AI_ADDRCONFIG
                           | AI_ADDRCONFIG  /* supported by this computer */
-                          | AI_V4MAPPED;   /* support IPv4 through IPv6 */
+#endif
+                          ;
     gai_return = getaddrinfo(hostname, port, &gai_hints, &gai_result);
     if (gai_return != 0) {
         if (gai_return == EAI_SYSTEM) {
@@ -888,3 +925,72 @@ static int parse_request(const char *request, char *host, char *port,
 
     return 0;
 }
+
+
+static void log_session_information(gnutls_session_t session) {
+    /* From doc/examples/ex-session-info.c of GnuTLS 3.2.3's tarball and
+     * modified, thanks. */
+
+    const char *tmp;
+    gnutls_credentials_type_t cred;
+    gnutls_kx_algorithm_t kx;
+    int dhe, ecdh;
+
+    dhe = 0;
+    ecdh = 0;
+
+    /* Key exchange algorithm. */
+    kx = gnutls_kx_get(session);
+    LOG(DEBUG2, "- key exchange: %s", gnutls_kx_get_name(kx));
+
+    /* Authentication type. */
+    cred = gnutls_auth_get_type(session);
+    switch (cred) {
+        case GNUTLS_CRD_CERTIFICATE:
+            if (kx == GNUTLS_KX_DHE_RSA
+                    || kx == GNUTLS_KX_DHE_DSS) {
+                dhe = 1;
+#ifdef GNUTLS_KX_ECDHE_RSA
+            } else if (kx == GNUTLS_KX_ECDHE_RSA
+                    || kx == GNUTLS_KX_ECDHE_ECDSA) {
+                ecdh = 1;
+#endif
+            }
+            break;
+
+        case GNUTLS_CRD_IA:
+        case GNUTLS_CRD_SRP:
+        case GNUTLS_CRD_PSK:
+        case GNUTLS_CRD_ANON:
+        default:
+            /* This shouldn't occur. */
+            LOG(WARNING, "unexpected authentication method: %d", cred);
+            break;
+    }
+
+    /* Information about key exchange. */
+    if (dhe) {
+        LOG(DEBUG2, "- ephemeral DH using prime of %d bits",
+                    gnutls_dh_get_prime_bits(session));
+    } else if (ecdh) {
+#ifdef GNUTLS_KX_ECDHE_RSA
+        LOG(DEBUG2, "- ephemeral ECDH using curve %s",
+                    gnutls_ecc_curve_get_name(gnutls_ecc_curve_get(session)));
+#endif
+    }
+
+    tmp = gnutls_protocol_get_name(gnutls_protocol_get_version(session));
+    LOG(DEBUG2, "- protocol: %s", tmp); /* e.g. TLS 1.0 */
+
+    tmp = gnutls_certificate_type_get_name(gnutls_certificate_type_get(session));
+    LOG(DEBUG2, "- certificate type: %s", tmp);
+
+    tmp = gnutls_compression_get_name(gnutls_compression_get(session));
+    LOG(DEBUG2, "- compression: %s", tmp);
+
+    tmp = gnutls_cipher_get_name(gnutls_cipher_get(session));
+    LOG(DEBUG2, "- cipher: %s", tmp);
+
+    tmp = gnutls_mac_get_name(gnutls_mac_get(session));
+    LOG(DEBUG2, "- MAC: %s", tmp);
+}