1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/* Copyright (c) 2008-2009, AbiSource Corporation B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of AbiSource Corporation B.V. nor the
* names of other contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ABISOURCE CORPORATION B.V. AND OTHER
* CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ABISOURCE
* CORPORATION B.V OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __TLS_TUNNEL_H__
#define __TLS_TUNNEL_H__
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <asio.hpp>
#include <string>
#include <vector>
#ifdef _MSC_VER
typedef long ssize_t;
typedef int pid_t;
#endif
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
namespace tls_tunnel {
typedef boost::shared_ptr<asio::ip::tcp::socket> socket_ptr_t;
typedef boost::shared_ptr<gnutls_session_t> session_ptr_t;
typedef boost::shared_ptr< std::vector<char> > buffer_ptr_t;
class Exception {
public:
Exception(const std::string& message);
const std::string& message() const;
private:
std::string message_;
};
class Transport : public boost::enable_shared_from_this<Transport> {
public:
asio::io_service& io_service();
void run();
void stop();
protected:
Transport();
virtual ~Transport();
private:
asio::io_service io_service_;
asio::io_service::work work_;
};
typedef boost::shared_ptr<Transport> transport_ptr_t;
class ClientTransport : public Transport {
public:
ClientTransport(const std::string& host, unsigned short port,
boost::function<void (transport_ptr_t, socket_ptr_t)> on_connect);
void connect();
private:
std::string host_;
unsigned short port_;
boost::function<void (transport_ptr_t, socket_ptr_t)> on_connect_;
};
class ServerTransport : public Transport {
public:
ServerTransport(const std::string& ip, unsigned short port,
boost::function<void (transport_ptr_t, socket_ptr_t)> on_connect);
void accept();
private:
void on_accept(const asio::error_code& error, socket_ptr_t socket_ptr);
asio::ip::tcp::acceptor acceptor_;
boost::function<void (transport_ptr_t, socket_ptr_t)> on_connect_;
};
class Proxy {
public:
virtual ~Proxy();
static bool tls_tunnel_init();
static void tls_tunnel_deinit();
virtual void setup() = 0;
void run();
virtual void stop();
protected:
Proxy(const std::string& ca_file);
void on_local_read(const asio::error_code& error, std::size_t bytes_transferred,
transport_ptr_t transport_ptr, session_ptr_t session_ptr, socket_ptr_t local_socket_ptr,
buffer_ptr_t local_buffer_ptr, socket_ptr_t remote_socket_ptr);
void tunnel(transport_ptr_t transport_ptr, session_ptr_t session_ptr,
socket_ptr_t local_socket_ptr, socket_ptr_t remote_socket_ptr);
void disconnect_(transport_ptr_t transport_ptr, session_ptr_t session_ptr,
socket_ptr_t local_socket_ptr, socket_ptr_t remote_socket_ptr);
gnutls_certificate_credentials_t x509cred;
transport_ptr_t transport_ptr_; // we only store this as a member so we are able to start/stop the transport
private:
void tunnel_(transport_ptr_t transport_ptr, session_ptr_t session_ptr,
socket_ptr_t local_socket_ptr, buffer_ptr_t local_buffer_ptr,
socket_ptr_t remote_socket);
asio::thread* t;
};
// FIXME: this clientproxy can only handle 1 SSL connection at the same time
class ClientProxy : public Proxy {
public:
ClientProxy(const std::string& connect_address, unsigned short connect_port,
const std::string& ca_file, bool check_hostname);
virtual void setup();
virtual void stop();
const std::string& local_address() const;
unsigned short local_port() const;
private:
void on_transport_connect(transport_ptr_t transport_ptr, socket_ptr_t remote_socket_ptr);
void on_client_connect(const asio::error_code& error, transport_ptr_t transport_ptr,
session_ptr_t session_ptr, socket_ptr_t local_socket_ptr, socket_ptr_t remote_socket_ptr);
session_ptr_t setup_tls_session(socket_ptr_t remote_socket_ptr);
std::string local_address_;
unsigned short local_port_;
std::string connect_address_;
unsigned short connect_port_;
boost::shared_ptr<asio::ip::tcp::acceptor> acceptor_ptr;
bool check_hostname_;
};
class ServerProxy : public Proxy {
public:
ServerProxy(const std::string& bind_ip, unsigned short bind_port, unsigned short local_port,
const std::string& ca_file, const std::string& cert_file, const std::string& key_file);
virtual void setup();
private:
void on_transport_connect(transport_ptr_t transport_ptr, socket_ptr_t remote_socket_ptr);
session_ptr_t setup_tls_session(socket_ptr_t remote_socket_ptr);
std::string bind_ip_;
unsigned short bind_port_;
unsigned short local_port_;
gnutls_dh_params_t dh_params;
};
} /* namespace tls_tunnel */
#endif /* __TLS_TUNNEL_H__ */
|