X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fconnection.c;h=a473873d65800ce890be26c5ffbc27fe989593d2;hb=988acc79a2e88342edf375f886cfd6cf098e4c9e;hp=aa42bfd2bffb60b9e8011b03a11c28bbbaf7013a;hpb=c51f4531e09a0fcf27aa9f3b5150fbadcf4f1a79;p=tlsproxy%2Ftlsproxy.git diff --git a/src/connection.c b/src/connection.c index aa42bfd..a473873 100644 --- a/src/connection.c +++ b/src/connection.c @@ -28,6 +28,8 @@ #include #include +#include + /* 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 @@ -59,7 +61,7 @@ static int initialize_tls_session_client(int peer_socket, static int initialize_tls_session_server(int peer_socket, gnutls_session_t *session, gnutls_certificate_credentials_t *x509_cred); -static int initialize_tls_session_both(int flags, +static int initialize_tls_session_both(unsigned int flags, int peer_socket, gnutls_session_t *session, gnutls_certificate_credentials_t *x509_cred); @@ -84,6 +86,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,18 +142,18 @@ 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; } if (parse_request(buffer, host, port, &version_minor) != 0) { - LOG(WARNING, "bad request: %s", buffer); + LOG(WARNING, "bad request: >%s<", buffer); send_bad_request(client_fd_write); 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 +196,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; } @@ -253,6 +257,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 +304,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 +349,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); } @@ -406,6 +416,11 @@ static int initialize_tls_session_client(int peer_socket, PROXY_CA_PATH); gnutls_certificate_free_credentials(*x509_cred); return -1; + } else if (result != 1) { + /* Must contain only one CA, our proxy CA. */ + LOG(ERROR, "initialize_tls_session_client(): multiple CAs found"); + gnutls_certificate_free_credentials(*x509_cred); + return -1; } } /* If the invalid hostname was specified do nothing, we use a self-signed @@ -457,28 +472,27 @@ static int initialize_tls_session_server(int peer_socket, return initialize_tls_session_both(GNUTLS_CLIENT, peer_socket, session, x509_cred); } -static int initialize_tls_session_both(int flags, +static int initialize_tls_session_both(unsigned int flags, int peer_socket, gnutls_session_t *session, gnutls_certificate_credentials_t *x509_cred) { int result; + *session = NULL; + result = gnutls_init(session, flags); if (result != GNUTLS_E_SUCCESS) { LOG(ERROR, "initialize_tls_session_both(): gnutls_init(): %s", gnutls_strerror(result)); - gnutls_certificate_free_credentials(*x509_cred); - return -1; + goto err; } result = gnutls_priority_set(*session, global_tls_priority_cache); if (result != GNUTLS_E_SUCCESS) { LOG(ERROR, "initialize_tls_session_both(): gnutls_priority_set(): %s", gnutls_strerror(result)); - gnutls_deinit(*session); - gnutls_certificate_free_credentials(*x509_cred); - return -1; + goto err; } result = gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, *x509_cred); @@ -486,9 +500,7 @@ static int initialize_tls_session_both(int flags, LOG(ERROR, "initialize_tls_session_both(): gnutls_credentials_set(): %s", gnutls_strerror(result)); - gnutls_deinit(*session); - gnutls_certificate_free_credentials(*x509_cred); - return -1; + goto err; } #ifdef HAVE_GNUTLS_TRANSPORT_SET_INT2 @@ -499,6 +511,13 @@ static int initialize_tls_session_both(int flags, #endif return 0; + +err: + if (*session) { + gnutls_deinit(*session); + } + gnutls_certificate_free_credentials(*x509_cred); + return -1; } @@ -542,14 +561,14 @@ static int read_http_request(FILE *client_fd, char *request, size_t length) { while (fgets(buffer, sizeof(buffer), client_fd) != NULL) { const char *authentication = "Proxy-Authorization: Basic "; - if (http_digest_authorization != NULL + if (global_http_digest_authorization != NULL && !strncmp(buffer, authentication, strlen(authentication))) { found_proxy_authorization = 1; /* Check if the passphrase matches. */ strtok(buffer, "\r\n"); if (strcmp(buffer + strlen(authentication), - http_digest_authorization)) { + global_http_digest_authorization)) { return -3; } } @@ -562,9 +581,11 @@ static int read_http_request(FILE *client_fd, char *request, size_t length) { if (ferror(client_fd)) { LOG_PERROR(WARNING, "read_http_request(): fgets()"); return -1; + } else if (feof(client_fd)) { + return -2; } - if (http_digest_authorization != NULL && !found_proxy_authorization) { + if (global_http_digest_authorization != NULL && !found_proxy_authorization) { return -3; } @@ -617,7 +638,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 */); @@ -702,7 +723,7 @@ 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", + LOG(DEBUG2, "transfer_data_tls(): suggested buffer size: %ld", (long int)buffer_size); for (;;) { @@ -877,3 +898,71 @@ 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: + /* 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); +}