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-2012 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 /* socket(), bind(), accept(), listen() */
27 #include <sys/types.h>
28 #include <sys/socket.h>
32 #include <arpa/inet.h>
43 GCRY_THREAD_OPTION_PTHREAD_IMPL;
46 /* Size of ringbuffer. */
47 #define RINGBUFFER_SIZE 10
49 /* Bit size of Diffie-Hellman key exchange parameters. */
53 /* For gnutls_*() functions. */
54 #define GNUTLS_ERROR_EXIT(error, message) \
55 if (GNUTLS_E_SUCCESS != error) { \
56 fprintf(stderr, "%s: %s\n", message, gnutls_strerror(error)); \
61 /* Server should shut down. Set by SIGINT handler. */
62 static volatile int done = 0;
64 /* Number of threads. */
65 static size_t thread_count;
67 /* Synchronized ring buffer storing accept()ed client sockets. */
68 static int ringbuffer[RINGBUFFER_SIZE];
69 static int ringbuffer_read;
70 static int ringbuffer_write;
71 static SEM *ringbuffer_full; /* At least one element in the buffer? */
72 static SEM *ringbuffer_free; /* Space for another element in the buffer? */
73 static SEM *ringbuffer_lock; /* Read lock. */
77 static void sigint_handler(int signal);
80 static void parse_arguments(int argc, char **argv);
81 static void print_usage(const char *argv);
83 static void initialize_gnutls(void);
84 static void deinitialize_gnutls(void);
86 static void worker_thread(void);
89 int main(int argc, char **argv) {
91 int client_socket, server_socket;
93 struct sockaddr_in server_in;
95 struct sockaddr_in6 server_in;
101 struct sigaction action;
103 parse_arguments(argc, argv);
105 port = atoi(argv[argc - 1]);
106 if (0 >= port || 0xffff < port) {
107 print_usage(argv[0]);
108 fprintf(stderr, "\ninvalid port\n");
112 sigemptyset(&action.sa_mask);
115 /* Setup our SIGINT signal handler which allows a "normal" termination of
116 * the server in DEBUG mode. */
117 action.sa_handler = sigint_handler;
118 sigaction(SIGINT, &action, NULL);
120 /* Ignore SIGPIPEs. */
121 action.sa_handler = SIG_IGN;
122 sigaction(SIGPIPE, &action, NULL);
124 /* Initialize ring buffer. */
126 ringbuffer_write = 0;
127 ringbuffer_full = sem_init(0);
128 ringbuffer_free = sem_init(RINGBUFFER_SIZE);
129 ringbuffer_lock = sem_init(1);
130 if (NULL == ringbuffer_full
131 || NULL == ringbuffer_free
132 || NULL == ringbuffer_lock) {
133 perror("sem_init()");
139 /* Spawn worker threads to handle requests. */
140 threads = (pthread_t *)malloc(thread_count * sizeof(pthread_t));
141 if (NULL == threads) {
142 perror("thread malloc failed");
145 for (i = 0; i < thread_count; i++) {
149 result = pthread_create(&thread, NULL,
150 (void * (*)(void *))&worker_thread,
153 fprintf(stderr, "failed to create worker thread: %s\n",
162 server_socket = socket(PF_INET, SOCK_STREAM, 0);
164 server_socket = socket(PF_INET6, SOCK_STREAM, 0);
166 if (-1 == server_socket) {
171 /* Fast rebinding for debug mode, could cause invalid packets. */
172 if (LOG_DEBUG_LEVEL <= global_log_level) {
173 int socket_option = 1;
174 setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR,
175 &socket_option, sizeof(socket_option));
178 /* Bind to the listen socket. */
179 memset(&server_in, 0, sizeof(server_in));
181 server_in.sin_family = AF_INET; /* IPv4 only */
182 server_in.sin_addr.s_addr = htonl(INADDR_ANY); /* bind to any address */
183 server_in.sin_port = htons((uint16_t)port); /* port to bind to */
185 server_in.sin6_family = AF_INET6; /* IPv6 (and IPv4) */
186 server_in.sin6_addr = in6addr_any; /* bind to any address */
187 server_in.sin6_port = htons((uint16_t)port); /* port to bind to */
189 if (-1 == bind(server_socket, (struct sockaddr *)&server_in,
190 sizeof(server_in))) {
194 /* And accept connections. */
195 if (-1 == listen(server_socket, 5)) {
200 if (LOG_DEBUG_LEVEL <= global_log_level) {
201 printf("Listening for connections on port %d.\n", port);
203 if (NULL != global_proxy_host && NULL != global_proxy_port) {
204 printf("Using proxy: %s:%s.\n", global_proxy_host,
210 /* Accept new connection. */
211 client_socket = accept(server_socket, NULL, NULL);
212 if (-1 == client_socket) {
217 /* No lock, we only have one producer! */
219 ringbuffer[ringbuffer_write] = client_socket;
220 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
224 close(server_socket);
226 /* Poison all threads and shut them down. */
227 for (i = 0; i < thread_count; i++) {
229 ringbuffer[ringbuffer_write] = -1; /* poison */
230 ringbuffer_write = (ringbuffer_write + 1) % RINGBUFFER_SIZE;
233 for (i = 0; i < thread_count; i++) {
234 errno = pthread_join(threads[i], NULL);
236 perror("pthread_join()");
241 free(ringbuffer_full);
242 free(ringbuffer_free);
243 free(ringbuffer_lock);
247 deinitialize_gnutls();
249 free(global_proxy_host);
250 free(global_proxy_port);
256 static void sigint_handler(int signal_number) {
263 static void parse_arguments(int argc, char **argv) {
266 /* Default values. */
269 global_log_level = LOG_DEBUG_LEVEL;
271 global_log_level = LOG_WARNING_LEVEL;
273 global_passthrough_unknown = 0;
275 while (-1 != (option = getopt(argc, argv, "d:p:t:uh?"))) {
278 if (0 > atoi(optarg)) {
279 print_usage(argv[0]);
280 fprintf(stderr, "\n-d positive number required\n");
283 global_log_level = atoi(optarg);
289 /* -p must have the format host:port. */
290 if (NULL == (position = strchr(optarg, ':'))
291 || position == optarg
292 || 0 == strlen(position + 1)
293 || 0 >= atoi(position + 1)
294 || 0xffff < atoi(position + 1)) {
295 print_usage(argv[0]);
296 fprintf(stderr, "\ninvalid -p, format host:port\n");
300 global_proxy_host = malloc((size_t)(position - optarg) + 1);
301 if (NULL == global_proxy_host) {
305 memcpy(global_proxy_host, optarg, (size_t)(position - optarg));
306 global_proxy_host[position - optarg] = '\0';
308 global_proxy_port = malloc(strlen(position + 1) + 1);
309 if (NULL == global_proxy_port) {
313 strcpy(global_proxy_port, position + 1);
318 if (0 >= atoi(optarg)) {
319 print_usage(argv[0]);
320 fprintf(stderr, "\n-t positive number required\n");
323 thread_count = (size_t)atoi(optarg);
327 global_passthrough_unknown = 1;
332 print_usage(argv[0]);
337 if (optind >= argc) {
338 print_usage(argv[0]);
339 fprintf(stderr, "\nport missing\n");
343 static void print_usage(const char *argv) {
344 fprintf(stderr, "Usage: %s [-d level] [-p host:port] [-t count] [-u] port\n",
346 fprintf(stderr, "\n");
347 fprintf(stderr, "-d debug level: 0=errors only, 2=debug [default: 1]\n");
348 fprintf(stderr, "-p proxy hostname and port\n");
349 fprintf(stderr, "-t number of threads [default: 10]\n");
350 fprintf(stderr, "-u passthrough connection if no certificate is stored \
351 [default: error]\n");
352 fprintf(stderr, " WARNING: might be a security problem!\n");
355 static void initialize_gnutls(void) {
357 gcry_error_t error = 0;
359 /* Thread safe setup. Must be called before gnutls_global_init(). */
360 error = gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
362 fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
363 gcry_strerror(error));
366 /* Prevent usage of blocking /dev/random. */
367 error = gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
369 fprintf(stderr, "gcry_control(): %s/%s\n", gcry_strsource(error),
370 gcry_strerror(error));
374 /* Initialize GnuTLS. */
375 result = gnutls_global_init();
376 GNUTLS_ERROR_EXIT(result, "gnutls_global_init()");
378 /* Setup GnuTLS cipher suites. */
379 result = gnutls_priority_init(&global_tls_priority_cache, "NORMAL", NULL);
380 GNUTLS_ERROR_EXIT(result, "gnutls_priority_init()");
382 /* Generate Diffie-Hellman parameters. */
383 result = gnutls_dh_params_init(&global_tls_dh_params);
384 GNUTLS_ERROR_EXIT(result, "gnutls_dh_params_init()");
385 result = gnutls_dh_params_generate2(global_tls_dh_params, DH_SIZE);
386 GNUTLS_ERROR_EXIT(result, "gnutls_dh_params_generate2()");
388 static void deinitialize_gnutls(void) {
389 gnutls_dh_params_deinit(global_tls_dh_params);
390 gnutls_priority_deinit(global_tls_priority_cache);
392 gnutls_global_deinit();
395 static void worker_thread(void) {
399 /* Get next element from ring buffer. */
402 client_socket = ringbuffer[ringbuffer_read];
403 ringbuffer_read = (ringbuffer_read + 1) % RINGBUFFER_SIZE;
407 /* Negative value indicates we should shut down our thread. */
408 if (0 > client_socket) {
412 handle_connection(client_socket);