2 * tlsproxy is a TLS proxy for HTTPS which intercepts the connections and
3 * ensures the server certificate doesn't change. Normally this isn't detected
4 * if a trusted CA for the new server certificate is installed.
6 * Copyright (C) 2011-2013 Simon Ruderich
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "connection.h"
26 #include <arpa/inet.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
34 /* Necessary for GnuTLS when used with threads. */
36 GCRY_THREAD_OPTION_PTHREAD_IMPL;
39 /* Size of ringbuffer. */
40 #define RINGBUFFER_SIZE 10
42 /* Bit size of Diffie-Hellman key exchange parameters. */
46 /* For gnutls_*() functions. */
47 #define GNUTLS_ERROR_EXIT(error, message) \
48 if (error != GNUTLS_E_SUCCESS) { \
49 fprintf(stderr, "%s: %s\n", message, gnutls_strerror(error)); \
54 /* Server should shut down. Set by SIGINT handler. */
55 static volatile int done = 0;
57 /* Number of threads. */
58 static size_t thread_count;
60 /* Synchronized ring buffer storing accept()ed client sockets. */
61 static int ringbuffer[RINGBUFFER_SIZE];
62 static int ringbuffer_read;
63 static int ringbuffer_write;
64 static SEM *ringbuffer_full; /* At least one element in the buffer? */
65 static SEM *ringbuffer_free; /* Space for another element in the buffer? */
66 static SEM *ringbuffer_lock; /* Read lock. */
70 static void sigint_handler(int signal);
73 static void parse_arguments(int argc, char **argv);
74 static void print_usage(const char *argv);
76 static void initialize_gnutls(void);
77 static void deinitialize_gnutls(void);
79 static void worker_thread(void);
82 int main(int argc, char **argv) {
84 int client_socket, server_socket;
86 struct sockaddr_in server_in;
88 struct sockaddr_in6 server_in;
94 struct sigaction action;
96 parse_arguments(argc, argv);
98 port = atoi(argv[argc - 1]);
99 if (port <= 0 || port > 0xffff ) {
100 print_usage(argv[0]);
101 fprintf(stderr, "\ninvalid port: '%s'\n", argv[argc - 1]);
105 sigemptyset(&action.sa_mask);
108 /* Setup our SIGINT signal handler which allows a "normal" termination of
109 * the server in DEBUG mode. */
110 action.sa_handler = sigint_handler;
111 sigaction(SIGINT, &action, NULL);
113 /* Ignore SIGPIPEs. */
114 action.sa_handler = SIG_IGN;
115 sigaction(SIGPIPE, &action, NULL);
117 /* Initialize ring buffer. */
119 ringbuffer_write = 0;
120 ringbuffer_full = sem_init(0);
121 ringbuffer_free = sem_init(RINGBUFFER_SIZE);
122 ringbuffer_lock = sem_init(1);
123 if (NULL == ringbuffer_full
124 || NULL == ringbuffer_free
125 || NULL == ringbuffer_lock) {
126 perror("sem_init()");
132 /* Spawn worker threads to handle requests. */
133 threads = malloc(thread_count * sizeof(*threads));
134 if (threads == NULL) {
135 perror("thread malloc failed");
138 for (i = 0; i < thread_count; i++) {
139 errno = pthread_create(threads + i, NULL,
140 (void * (*)(void *))&worker_thread,
143 perror("failed to create worker thread");
149 server_socket = socket(PF_INET, SOCK_STREAM, 0);
151 server_socket = socket(PF_INET6, SOCK_STREAM, 0);
153 if (server_socket == -1) {
158 /* Fast rebinding for debug mode, could cause invalid packets. */
159 if (global_log_level >= LOG_DEBUG_LEVEL) {
160 int socket_option = 1;
161 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
162 &socket_option, sizeof(socket_option));
165 /* Bind to the listen socket. */
166 memset(&server_in, 0, sizeof(server_in));
168 server_in.sin_family = AF_INET; /* IPv4 only */
169 server_in.sin_addr.s_addr = htonl(INADDR_ANY); /* bind to any address */
170 server_in.sin_port = htons((uint16_t)port); /* port to bind to */
172 server_in.sin6_family = AF_INET6; /* IPv6 (and IPv4) */
173 server_in.sin6_addr = in6addr_any; /* bind to any address */
174 server_in.sin6_port = htons((uint16_t)port); /* port to bind to */
176 if (bind(server_socket, (struct sockaddr *)&server_in,
177 sizeof(server_in)) == -1) {
181 /* And accept connections. */
182 if (listen(server_socket, 5) == -1) {
187 if (global_log_level >= LOG_DEBUG_LEVEL) {
188 printf("tlsproxy %s\n", VERSION);
189 printf("Listening for connections on port %d.\n", port);
191 if (global_proxy_host != NULL && global_proxy_port != NULL) {
192 printf("Using proxy: %s:%s.\n", global_proxy_host,
198 /* Accept new connection. */
199 client_socket = accept(server_socket, NULL, NULL);
200 if (client_socket == -1) {
205 /* No lock necessary, we only have one producer! */
207 ringbuffer[ringbuffer_write] = client_socket;
208 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
212 close(server_socket);
214 /* Poison all threads and shut them down. */
215 for (i = 0; i < thread_count; i++) {
217 ringbuffer[ringbuffer_write] = -1; /* poison */
218 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
221 for (i = 0; i < thread_count; i++) {
222 errno = pthread_join(threads[i], NULL);
224 perror("pthread_join()");
228 free(ringbuffer_full);
229 free(ringbuffer_free);
230 free(ringbuffer_lock);
234 deinitialize_gnutls();
236 free(global_proxy_host);
237 free(global_proxy_port);
243 static void sigint_handler(int signal_number) {
250 static void parse_arguments(int argc, char **argv) {
253 /* Default values. */
256 global_log_level = LOG_DEBUG_LEVEL;
258 global_log_level = LOG_WARNING_LEVEL;
260 global_passthrough_unknown = 0;
262 while ((option = getopt(argc, argv, "d:p:t:uh?")) != -1) {
265 global_log_level = atoi(optarg);
266 if (global_log_level < 0) {
267 print_usage(argv[0]);
268 fprintf(stderr, "\n-d positive number required: '%s'\n",
277 /* -p must have the format host:port. */
278 if ((position = strchr(optarg, ':')) == NULL
279 || optarg == position
280 || strlen(position + 1) == 0
281 || atoi(position + 1) <= 0
282 || atoi(position + 1) > 0xffff) {
283 print_usage(argv[0]);
284 fprintf(stderr, "\ninvalid -p: '%s', format host:port\n",
289 global_proxy_host = malloc((size_t)(position - optarg) + 1);
290 if (global_proxy_host == NULL) {
294 memcpy(global_proxy_host, optarg, (size_t)(position - optarg));
295 global_proxy_host[position - optarg] = '\0';
297 global_proxy_port = malloc(strlen(position + 1) + 1);
298 if (global_proxy_port == NULL) {
302 strcpy(global_proxy_port, position + 1);
307 if (atoi(optarg) <= 0) {
308 print_usage(argv[0]);
309 fprintf(stderr, "\n-t positive number required: '%s'\n",
313 thread_count = (size_t)atoi(optarg);
317 global_passthrough_unknown = 1;
322 print_usage(argv[0]);
327 if (optind >= argc) {
328 print_usage(argv[0]);
329 fprintf(stderr, "\nport missing\n");
333 static void print_usage(const char *argv) {
334 fprintf(stderr, "tlsproxy %s, a certificate checking TLS proxy\n",
336 fprintf(stderr, "Usage: %s [-d level] [-p host:port] [-t count] [-u] port\n",
338 fprintf(stderr, "\n");
339 fprintf(stderr, "-d debug level: 0=errors only, 2=debug [default: 1]\n");
340 fprintf(stderr, "-p proxy hostname and port\n");
341 fprintf(stderr, "-t number of threads [default: 10]\n");
342 fprintf(stderr, "-u passthrough connection if no certificate is stored \
343 [default: error]\n");
344 fprintf(stderr, " WARNING: might be a security problem!\n");
347 static void initialize_gnutls(void) {
351 /* Thread safe setup. Must be called before gnutls_global_init(). */
352 error = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
354 fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
355 gcry_strerror(error));
358 /* Prevent usage of blocking /dev/random. */
359 error = gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
361 fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
362 gcry_strerror(error));
366 /* Initialize GnuTLS. */
367 result = gnutls_global_init();
368 GNUTLS_ERROR_EXIT(result, "gnutls_global_init()");
370 /* Setup GnuTLS cipher suites. */
371 result = gnutls_priority_init(&global_tls_priority_cache, "NORMAL", NULL);
372 GNUTLS_ERROR_EXIT(result, "gnutls_priority_init()");
374 /* Generate Diffie-Hellman parameters. */
375 result = gnutls_dh_params_init(&global_tls_dh_params);
376 GNUTLS_ERROR_EXIT(result, "gnutls_dh_params_init()");
377 result = gnutls_dh_params_generate2(global_tls_dh_params, DH_SIZE);
378 GNUTLS_ERROR_EXIT(result, "gnutls_dh_params_generate2()");
380 static void deinitialize_gnutls(void) {
381 gnutls_dh_params_deinit(global_tls_dh_params);
382 gnutls_priority_deinit(global_tls_priority_cache);
384 gnutls_global_deinit();
387 static void worker_thread(void) {
391 /* Get next element from ring buffer. */
394 client_socket = ringbuffer[ringbuffer_read];
395 ringbuffer_read = (ringbuffer_read + 1) % RINGBUFFER_SIZE;
399 /* Negative value indicates we should shut down our thread. */
400 if (client_socket < 0) {
404 handle_connection(client_socket);