Files
OpenVPNAdapter/openvpn/transport/client/httpcli.hpp
T
Sergey Abramchuk 5edb23a7ab Squashed 'Sources/OpenVPNAdapter/Libraries/Vendors/openvpn/' changes from daf575ff50..275cf80efb
275cf80efb mac/tuncli: Don't take address of temporary error.
1406187bfc tun/win/tunutil: Don't auto& a temporary iterator.
fe7f984c5d ip/ping6: Use _WIN32, not _MSC_VER (to fix MinGW).
03a906771e win: add OpenSSL as solution configuration
89cc11b300 win: enable building Windows client with OpenSSL
febb24e7d9 openssl/compat.hpp: remove functions already defined in OpenSSL
0833eb1f76 linux/tunsetup: Fix missing asio/errinfo declaration
d54b742910 linux: Improve cpu_time() using glibc/kernel methods
a55fe2b554 tests: Added unit test for linux/cputime.hpp
e33a00e6de [OVPN3-431] agent: Wintun support for agent
42592eb1b1 appveyor: initial commit
3e3f2078e6 win: rename env var in project file
a2496a3616 Wintun: experimental support
58a7866b45 build script: added OPENSSL_DIST parameter to specify a custom OpenSSL build
288ea0277e OpenSSLContext: SSL_CTX_set_ecdh_auto() becomes a no-op in OpenSSL 1.1, so #ifdef out to avoid compiler warnings
3ef5059fa6 TLSSessionTicketBase: removed the ERROR symbol from a local enum in case it conflicts with a global preprocessor symbol
3364ed76b8 TLSSessionTicketBase: removed trailing comma from Status enum
025c7bad88 mbedtls/sslctx: Fix missing override in virtual methods
6cb3243681 mbedTLS: ssl() method accepting hostname should check if it is null
ca31da7d28 bio_memq_stream.hpp: fixed multi-thread race (introduced with OpenSSL 1.1 support) using init_static() approach
2deb402223 OpenSSLContext::tls_ticket_key_callback: get self with SSL_get_ex_data instead of ssl->ctx->app_verify_arg
eec139a100 MSF::find: renamed template type names to avoid conflict with preprocessor symbol (ITER) in test/ssl/proto.cpp
1024d37f33 str_neq: fixed bug where neq was not initialized
c00b6f6302 Listen::List: refactored and extended expand_ports()
448c549a0b cpu_time(): added bool thread parameter to return CPU time of current thread (instead of process)
868801d7d9 Linux library: added cpu_time() method to return the CPU time of the current process
964d2cd428 SSL layer: added did_full_handshake() method and implemented for OpenSSL
dd18d6c806 crypto::str_neq: use atomic_thread_fence(std::memory_order_acq_rel) instead of OPENVPN_COMPILER_FENCE
6a30af9528 OpenSSLSessionCache: use map instead of unordered_map
3ecbcbc81b OptionList: fixed compile errors that occur when get_num<T>() is used with a const type
72e9f858e4 SSL: added SSLConst::PEER_CERT_OPTIONAL flag and implemented for OpenSSL
33f15c8840 OpenSSL: use OPENSSL_VERSION_NUMBER instead of SSLEAY_VERSION_NUMBER
cadb712ea9 ProfileMerge: added "static-key" to is_fileref_directive()
85befa316a TLS session tickets: work around an issue in OpenSSL session ticket keying callback
f43c4c1440 TLSSessionTicketBase: misc fixes/enhancements
c5f4d59d39 OpenSSLContext: added missing X509_free() to rebuild_authcert()
658fcc50eb OptionList: added get_num methods with min/max but no default
162eeaa485 SSL layer: added RFC 5077 TLS session resumption ticket support
e0a821ddd6 OpenSSLContext: use C++11 member initializers
1ea5acce3c OpenSSLContext: minor changes to handshake_details()
74c0a4f995 string: added copy_fill() method
3e5921c06d AuthCert: added is_uninitialized() method
3d6b6b2319 library: added convenience method MSF::find() for maps/sets
18f5f4d1b5 SSLConfigAPI: remove set_enable_renegotiation()
18dcfd616c Added crypto::str_neq() function for securely comparing variable-length strings
4fc5725b9e RunContext: added get_servers() method
ae22f155fd server: determine when server-side session ID should be preserved on soon-to-be-closed connections
5e34759d50 client: HALT/RESTART message was not properly purging the Session ID when required
e1647eb407 Fix builds with GCC 4.8 compilers
b55f78dd1d test_sitnl.cpp: account for old iptools output
236d39258b Allow overriding DEP_DIR by environment variable
d56e049ea4 Refactor dependencies to be in a cmake script
e9dc75ec90 sitnl: add unit tests
faad8454be sitnl: pick the best gw by longest prefix and lowest metric
dfcc4bc437 [OVPN3-354] cli.cpp: support for round-robin DNS and redirect gw
8a502f3b61 [OVPN3-354] tun linux: support for round-robin DNS and redirect gw
c9315c7dc1 gwnetlink.hpp: specify destination when looking for gateway
89f091daf0 sitnl: implement interface filtering when looking for gateway
220de072a2 sitnl: support for multipart messages
5771dfc0ee transport: remove ip_hole_punch API
d448b4a7db tun/builder/client.hpp: use "override" method specifier
d85e92621d Make reproducible builds possible
7150f72e09 tun: remove code duplications in Linux tun implementations
8112f0cd7c [OVPN3-378] cli: support for TunBuilder API
6f0e9f6388 Fix Asio 0003 patch.
964662bacb Add /bigobj to build.py
74e40a8907 Upgrade ASIO to 0.13.0
a2713ce1f6 PureTLS: enable SNI by default when configuring client
19a44dbdda Merge branch 'qa'
a5fdf43726 InitProcess: comment clarification that crypto_init declaration causes SSL library init when instantiated
dec3bc140e OpenSSL: Revert a commit that breaks OpenSSL initialization
16a4e3d4a7 [OVPN3-405] asio: A quick fix for incorrect error message encoding
aa785c30c1 Fix Base64::UCharWrap compiler warnings
51a1469e6b Merge various fixes
218cfa39cb Explicitly disable TAP support when parsing configurations
3a0e768ecd Explicitly disable any potential TAP support
aba98471fc Fix base64 unit test with mbedtls and windows
9f84174f0b Add unit tests for Base64
017bc545ce Add base64 decode for void* data
452a353b2d Fix lzo build script to use it as dependency for the unit tests
dfdd528dc1 Convert unit test to Googletest
bd9ee482e6 Add copyright header to test_comp
059f20f2b2 Move compression unit test from common to core repository
5a024cde5c Added Snappy corpus for testing compression/decompression.
ec4d400933 Add compatibility functions for OpenSSL 1.1.0
9768562a01 OpenSSL 1.1: Add argument to external sign to specify algorithm
1bbd2cc78c OpenSSL 1.1: Replace RSA_F_RSA_EAY_PRIVATE_ENCRYPT with Openssl variant
c959a3cff0 OpenSSL 1.1: Replace remaining direct access to members
4307f024ca OpenSSL 1.1: And missing remaining compat implementations
3385c45151 OpenSSL 1.1: Use opaque pointer for HMAC_CTX
f29453f4ca OpenSSL 1.1: Add compat includes for HMAC
c107a1f6ab OpenSSL 1.1: Remove support for OpenSSL older than 1.0.0
024a10adc2 OpenSSL 1.1: Use EVP_MD_ctx as opaque pointer
35d82906c4 OpenSSL 1.1: Change EVP_CIPHER ctx field to pointer
ebf4b7e87d OpenSSL 1.1: Use X509_digest to get certificate digest
7d3e5d02f2 OpenSSL 1.1: Use SSL_get_ex_data instead of direct access
8717f822ca OpenSSL 1.1: Replace ctx->current with X509_STORE_CTX_get_current_cert
67fbe1ab3f OpenSSL 1.1: Use X509_check_purpose to check certificate types
7b5a92d58e OpenSSL 1.1: Change OpenSSL TLS version logic to match mbed TLS
c28b7d1893 OpenSSL 1.1: Adjust default OpenSSL cipher suites
f108044a09 OpenSSL 1.1: Add defines for TLS 1.3 in tlsver.hpp
ee1308b505 OpenSSL 1.1: Replace initialisation of RSA_meth with access method
905d681af1 OpenSSL 1.1: Use standard tls methods
cf28e4600c OpenSSL 1.1: Change BIO wrappers around to use access methods
5e6571163d OpenSSL 1.1: Implement compat methods for new BIO methods in 1.0.2
8837539a73 Use std::nothrow as argument for new
e6ec025932 Merge branch 'qa'
752a38c067 [OVPN3-397] size.hpp: wrap typedef in guards
d4e50f8c54 Merge branch 'qa'
d8d14e1991 [UCONNECT-1027] implement ResolveThread and ensure it is properly detachable
525a9a88a6 Merge branch qa
30ea53cb92 Replace custom memcpy implementation
de7c672ee7 Workaround for compiler bug in memneq
84fcecd5e7 Fix missing override annotation in udp/tcp/httpcli
1a3a69a496 [UCONNECT-1027] use one AsioWork object for the whole pre-resolve opertation
c4cbf93f9b Revert "[UCONNECT-1027] remotelist: create standalone object for resolve thread"
6ef089164e Allow unit tests to be also compiled with mbed TLS and on Windows
7c67bf7f50 Add unit tests for route emulation and establish common test suite
64a7b2f124 Add build file for core unit tests
0a0d080a49 Implement allowing local LAN access
2105b4b7c0 Fix Android route exclusion emulation

git-subtree-dir: Sources/OpenVPNAdapter/Libraries/Vendors/openvpn
git-subtree-split: 275cf80efb7a08adc920f7ca49075c776e596b08
2019-06-17 09:44:01 +03:00

1037 lines
27 KiB
C++

// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// HTTP proxy transport object.
#ifndef OPENVPN_TRANSPORT_CLIENT_HTTPCLI_H
#define OPENVPN_TRANSPORT_CLIENT_HTTPCLI_H
#include <vector>
#include <string>
#include <sstream>
#include <algorithm> // for std::min
#include <memory>
#include <openvpn/io/io.hpp>
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/string.hpp>
#include <openvpn/common/base64.hpp>
#include <openvpn/common/split.hpp>
#include <openvpn/common/options.hpp>
#include <openvpn/common/number.hpp>
#include <openvpn/common/userpass.hpp>
#include <openvpn/buffer/bufstr.hpp>
#include <openvpn/buffer/buflimit.hpp>
#include <openvpn/transport/tcplink.hpp>
#include <openvpn/transport/client/transbase.hpp>
#include <openvpn/transport/socket_protect.hpp>
#include <openvpn/transport/protocol.hpp>
#include <openvpn/http/reply.hpp>
#include <openvpn/http/status.hpp>
#include <openvpn/http/htmlskip.hpp>
#include <openvpn/proxy/proxyauth.hpp>
#include <openvpn/proxy/httpdigest.hpp>
#include <openvpn/proxy/ntlm.hpp>
#include <openvpn/client/remotelist.hpp>
#include <openvpn/crypto/digestapi.hpp>
namespace openvpn {
namespace HTTPProxyTransport {
class Options : public RC<thread_safe_refcount>
{
public:
struct CustomHeader : public RC<thread_unsafe_refcount>
{
typedef RCPtr<CustomHeader> Ptr;
std::string p1;
std::string p2;
};
struct CustomHeaderList : public std::vector<CustomHeader::Ptr>
{
};
typedef RCPtr<Options> Ptr;
Options() : allow_cleartext_auth(false) {}
RemoteList::Ptr proxy_server;
std::string username;
std::string password;
bool allow_cleartext_auth;
std::string http_version;
std::string user_agent;
CustomHeaderList headers;
void set_proxy_server(const std::string& host, const std::string& port)
{
proxy_server.reset(new RemoteList(host, port, Protocol(Protocol::TCP), "http proxy port"));
}
void proxy_server_set_enable_cache(const bool enable_cache)
{
proxy_server->set_enable_cache(enable_cache);
}
void proxy_server_precache(RemoteList::Ptr& r)
{
if (proxy_server->get_enable_cache())
r = proxy_server;
}
static Ptr parse(const OptionList& opt)
{
if (opt.exists("http-proxy"))
{
Ptr obj(new Options);
if (obj->parse_options(opt))
return obj;
}
return Ptr();
}
private:
bool parse_options(const OptionList& opt)
{
const Option* hp = opt.get_ptr("http-proxy");
if (hp)
{
// get server/port
set_proxy_server(hp->get(1, 256), hp->get(2, 16));
// get creds
{
std::vector<std::string> user_pass;
if (UserPass::parse(opt, "http-proxy-user-pass", 0, &user_pass))
{
if (user_pass.size() >= 1)
username = user_pass[0];
if (user_pass.size() >= 2)
password = user_pass[1];
}
}
// allow cleartext auth?
allow_cleartext_auth = (hp->get_optional(3, 16) != "auto-nct");
// get options
const OptionList::IndexList* hpo = opt.get_index_ptr("http-proxy-option");
if (hpo)
{
for (OptionList::IndexList::const_iterator i = hpo->begin(); i != hpo->end(); ++i)
{
const Option& o = opt[*i];
const std::string& type = o.get(1, 64);
if (type == "VERSION")
{
http_version = o.get(2, 16);
o.touch();
}
else if (type == "AGENT")
{
user_agent = o.get(2, 256);
o.touch();
}
else if (type == "EXT1" || type == "EXT2" || type == "CUSTOM-HEADER")
{
CustomHeader::Ptr h(new CustomHeader());
h->p1 = o.get(2, 512);
h->p2 = o.get_optional(3, 512);
headers.push_back(h);
o.touch();
}
}
}
return true;
}
else
return false;
}
};
class ClientConfig : public TransportClientFactory
{
public:
typedef RCPtr<ClientConfig> Ptr;
RemoteList::Ptr remote_list;
size_t free_list_max_size;
Frame::Ptr frame;
SessionStats::Ptr stats;
Options::Ptr http_proxy_options;
RandomAPI::Ptr rng; // random data source
DigestFactory::Ptr digest_factory; // needed by proxy auth methods
SocketProtect* socket_protect;
bool skip_html;
static Ptr new_obj()
{
return new ClientConfig;
}
virtual TransportClient::Ptr new_transport_client_obj(openvpn_io::io_context& io_context,
TransportClientParent* parent);
private:
ClientConfig()
: free_list_max_size(8),
socket_protect(nullptr),
skip_html(false)
{}
};
class Client : public TransportClient, AsyncResolvableTCP
{
typedef RCPtr<Client> Ptr;
typedef TCPTransport::Link<openvpn_io::ip::tcp, Client*, false> LinkImpl;
friend class ClientConfig; // calls constructor
friend LinkImpl::Base; // calls tcp_read_handler
public:
void transport_start() override
{
if (!impl)
{
if (!config->http_proxy_options)
{
parent->proxy_error(Error::PROXY_ERROR, "http_proxy_options not defined");
return;
}
halt = false;
// Get target server host:port. We don't care about resolving it
// since proxy server will do that for us.
remote_list().endpoint_available(&server_host, &server_port, nullptr);
// Get proxy server host:port, and resolve it if not already cached
if (proxy_remote_list().endpoint_available(&proxy_host, &proxy_port, nullptr))
{
// already cached
start_connect_();
}
else
{
// resolve it
parent->transport_pre_resolve();
async_resolve_name(proxy_host, proxy_port);
}
}
}
bool transport_send_const(const Buffer& buf) override
{
return send_const(buf);
}
bool transport_send(BufferAllocated& buf) override
{
return send(buf);
}
bool transport_send_queue_empty() override
{
if (impl)
return impl->send_queue_empty();
else
return false;
}
bool transport_has_send_queue() override
{
return true;
}
void transport_stop_requeueing() override { }
unsigned int transport_send_queue_size() override
{
if (impl)
return impl->send_queue_size();
else
return 0;
}
void reset_align_adjust(const size_t align_adjust) override
{
if (impl)
impl->reset_align_adjust(align_adjust);
}
void server_endpoint_info(std::string& host, std::string& port, std::string& proto, std::string& ip_addr) const override
{
host = server_host;
port = server_port;
const IP::Addr addr = server_endpoint_addr();
proto = "TCP";
proto += addr.version_string();
proto += "-via-HTTP";
ip_addr = addr.to_string();
}
IP::Addr server_endpoint_addr() const override
{
return IP::Addr::from_asio(server_endpoint.address());
}
Protocol transport_protocol() const override
{
if (server_endpoint.address().is_v4())
return Protocol(Protocol::TCPv4);
else if (server_endpoint.address().is_v6())
return Protocol(Protocol::TCPv6);
else
return Protocol();
}
void stop() override { stop_(); }
virtual ~Client() { stop_(); }
private:
struct ProxyResponseLimit : public BufferLimit<size_t>
{
ProxyResponseLimit() : BufferLimit(1024, 65536) {}
virtual void bytes_exceeded() {
OPENVPN_THROW_EXCEPTION("HTTP proxy response too large (> " << max_bytes << " bytes)");
}
virtual void lines_exceeded() {
OPENVPN_THROW_EXCEPTION("HTTP proxy response too large (> " << max_lines << " lines)");
}
};
Client(openvpn_io::io_context& io_context_arg,
ClientConfig* config_arg,
TransportClientParent* parent_arg)
: AsyncResolvableTCP(io_context_arg),
socket(io_context_arg),
config(config_arg),
parent(parent_arg),
resolver(io_context_arg),
halt(false),
n_transactions(0),
proxy_established(false),
http_reply_status(HTTP::ReplyParser::pending),
ntlm_phase_2_response_pending(false),
drain_content_length(0)
{
}
void transport_reparent(TransportClientParent* parent_arg) override
{
parent = parent_arg;
}
bool send_const(const Buffer& cbuf)
{
if (impl)
{
BufferAllocated buf(cbuf, 0);
return impl->send(buf);
}
else
return false;
}
bool send(BufferAllocated& buf)
{
if (impl)
return impl->send(buf);
else
return false;
}
void tcp_error_handler(const char *error) // called by LinkImpl and internally
{
std::ostringstream os;
os << "Transport error on '" << server_host << "' via HTTP proxy " << proxy_host << ':' << proxy_port << " : " << error;
stop();
parent->transport_error(Error::TRANSPORT_ERROR, os.str());
}
void proxy_error(const Error::Type fatal_err, const std::string& what)
{
std::ostringstream os;
os << "on " << proxy_host << ':' << proxy_port << ": " << what;
stop();
parent->proxy_error(fatal_err, os.str());
}
bool tcp_read_handler(BufferAllocated& buf) // called by LinkImpl
{
if (proxy_established)
{
if (!html_skip)
parent->transport_recv(buf);
else
drain_html(buf); // skip extraneous HTML after header
}
else
{
try {
proxy_read_handler(buf);
}
catch (const std::exception& e)
{
proxy_error(Error::PROXY_ERROR, e.what());
}
}
return true;
}
void tcp_write_queue_needs_send() // called by LinkImpl
{
if (proxy_established)
parent->transport_needs_send();
}
void tcp_eof_handler() // called by LinkImpl
{
if (proxy_established)
{
config->stats->error(Error::NETWORK_EOF_ERROR);
tcp_error_handler("NETWORK_EOF_ERROR");
}
else
{
try {
proxy_eof_handler();
}
catch (const std::exception& e)
{
proxy_error(Error::PROXY_ERROR, e.what());
}
}
}
void proxy_read_handler(BufferAllocated& buf)
{
// for anti-DoS, only allow a maximum number of chars in HTTP response
proxy_response_limit.add(buf);
if (http_reply_status == HTTP::ReplyParser::pending)
{
OPENVPN_LOG_NTNL("FROM PROXY: " << buf_to_string(buf));
for (size_t i = 0; i < buf.size(); ++i)
{
http_reply_status = http_parser.consume(http_reply, (char)buf[i]);
if (http_reply_status != HTTP::ReplyParser::pending)
{
buf.advance(i+1);
if (http_reply_status == HTTP::ReplyParser::success)
{
//OPENVPN_LOG("*** HTTP header parse complete, resid_size=" << buf.size());
//OPENVPN_LOG(http_reply.to_string());
// we are connected, switch socket to tunnel mode
if (http_reply.status_code == HTTP::Status::Connected)
{
if (config->skip_html)
{
proxy_half_connected();
html_skip.reset(new HTTP::HTMLSkip());
drain_html(buf);
}
else
proxy_connected(buf, true);
}
else if (ntlm_phase_2_response_pending)
ntlm_auth_phase_2_pre();
}
else
{
throw Exception("HTTP proxy header parse error");
}
break;
}
}
}
// handle draining of content controlled by Content-length header
if (drain_content_length)
{
const size_t drain = std::min(drain_content_length, buf.size());
buf.advance(drain);
drain_content_length -= drain;
if (!drain_content_length)
{
if (ntlm_phase_2_response_pending)
ntlm_auth_phase_2();
}
}
}
void proxy_connected(BufferAllocated& buf, const bool notify_parent)
{
proxy_established = true;
if (parent->transport_is_openvpn_protocol())
{
// switch socket from HTTP proxy handshake mode to OpenVPN protocol mode
impl->set_raw_mode(false);
if (notify_parent)
parent->transport_connecting();
try {
impl->inject(buf);
}
catch (const std::exception& e)
{
proxy_error(Error::PROXY_ERROR, std::string("post-header inject error: ") + e.what());
return;
}
}
else
{
if (notify_parent)
parent->transport_connecting();
parent->transport_recv(buf);
}
}
// Called after header received but before possible extraneous HTML
// is drained. At this point, we are in a state where output data
// (if OpenVPN protocol) is packetized, but input data is still in
// raw mode as we search the input stream for the end of the
// extraneous HTML. When we reach the beginning of payload data,
// proxy_connected() should be called with notify_parent == false.
void proxy_half_connected()
{
proxy_established = true;
if (parent->transport_is_openvpn_protocol())
impl->set_raw_mode_write(false);
parent->transport_connecting();
}
void drain_html(BufferAllocated& buf)
{
while (!buf.empty())
{
switch (html_skip->add(buf.pop_front()))
{
case HTTP::HTMLSkip::MATCH:
case HTTP::HTMLSkip::NOMATCH:
{
OPENVPN_LOG("Proxy: Skipped " << html_skip->n_bytes() << " byte(s) of HTML");
html_skip->get_residual(buf);
html_skip.reset();
proxy_connected(buf, false);
return;
}
case HTTP::HTMLSkip::PENDING:
break;
}
}
}
HTTPProxy::ProxyAuthenticate::Ptr get_proxy_authenticate_header(const char *type)
{
for (HTTP::HeaderList::const_iterator i = http_reply.headers.begin(); i != http_reply.headers.end(); ++i)
{
const HTTP::Header& h = *i;
if (string::strcasecmp(h.name, "proxy-authenticate") == 0)
{
HTTPProxy::ProxyAuthenticate::Ptr pa = new HTTPProxy::ProxyAuthenticate(h.value);
if (string::strcasecmp(type, pa->method) == 0)
return pa;
}
}
return HTTPProxy::ProxyAuthenticate::Ptr();
}
void proxy_eof_handler()
{
if (http_reply_status == HTTP::ReplyParser::success)
{
if (http_reply.status_code == HTTP::Status::ProxyAuthenticationRequired)
{
if (n_transactions <= 1)
{
//OPENVPN_LOG("*** PROXY AUTHENTICATION REQUIRED");
if (config->http_proxy_options->username.empty())
{
proxy_error(Error::PROXY_NEED_CREDS, "HTTP proxy requires credentials");
return;
}
HTTPProxy::ProxyAuthenticate::Ptr pa;
// NTLM
pa = get_proxy_authenticate_header("ntlm");
if (pa)
{
ntlm_auth_phase_1(*pa);
return;
}
// Digest
pa = get_proxy_authenticate_header("digest");
if (pa)
{
digest_auth(*pa);
return;
}
// Basic
pa = get_proxy_authenticate_header("basic");
if (pa)
{
if (config->http_proxy_options->allow_cleartext_auth)
{
basic_auth(*pa);
return;
}
else
throw Exception("HTTP proxy Basic authentication not allowed by user preference");
}
throw Exception("HTTP proxy-authenticate method must be Basic, Digest, or NTLM");
}
else
{
proxy_error(Error::PROXY_NEED_CREDS, "HTTP proxy credentials were not accepted");
return;
}
}
else if (http_reply.status_code == HTTP::Status::ProxyError
|| http_reply.status_code == HTTP::Status::NotFound
|| http_reply.status_code == HTTP::Status::ServiceUnavailable)
{
// this is a nonfatal error, so we pass Error::UNDEF to tell the upper layer to
// retry the connection
proxy_error(Error::UNDEF, "HTTP proxy server could not connect to OpenVPN server");
return;
}
else if (http_reply.status_code == HTTP::Status::Forbidden)
OPENVPN_THROW_EXCEPTION("HTTP proxy returned Forbidden status code");
else
OPENVPN_THROW_EXCEPTION("HTTP proxy status code: " << http_reply.status_code);
}
else if (http_reply_status == HTTP::ReplyParser::pending)
throw Exception("HTTP proxy unexpected EOF: reply incomplete");
else
throw Exception("HTTP proxy general error");
}
void basic_auth(HTTPProxy::ProxyAuthenticate& pa)
{
OPENVPN_LOG("Proxy method: Basic" << std::endl << pa.to_string());
std::ostringstream os;
gen_headers(os);
os << "Proxy-Authorization: Basic "
<< base64->encode(config->http_proxy_options->username + ':' + config->http_proxy_options->password)
<< "\r\n";
http_request = os.str();
reset();
start_connect_();
}
void digest_auth(HTTPProxy::ProxyAuthenticate& pa)
{
try {
OPENVPN_LOG("Proxy method: Digest" << std::endl << pa.to_string());
// constants
const std::string http_method = "CONNECT";
const std::string nonce_count = "00000001";
const std::string qop = "auth";
// get values from Proxy-Authenticate header
const std::string realm = pa.parms.get_value("realm");
const std::string nonce = pa.parms.get_value("nonce");
const std::string algorithm = pa.parms.get_value("algorithm");
const std::string opaque = pa.parms.get_value("opaque");
// generate a client nonce
unsigned char cnonce_raw[8];
config->rng->assert_crypto();
config->rng->rand_bytes(cnonce_raw, sizeof(cnonce_raw));
const std::string cnonce = render_hex(cnonce_raw, sizeof(cnonce_raw));
// build URI
const std::string uri = server_host + ":" + server_port;
// calculate session key
const std::string session_key = HTTPProxy::Digest::calcHA1(
*config->digest_factory,
algorithm,
config->http_proxy_options->username,
realm,
config->http_proxy_options->password,
nonce,
cnonce);
// calculate response
const std::string response = HTTPProxy::Digest::calcResponse(
*config->digest_factory,
session_key,
nonce,
nonce_count,
cnonce,
qop,
http_method,
uri,
"");
// generate proxy request
std::ostringstream os;
gen_headers(os);
os << "Proxy-Authorization: Digest username=\"" << config->http_proxy_options->username << "\", realm=\"" << realm << "\", nonce=\"" << nonce << "\", uri=\"" << uri << "\", qop=" << qop << ", nc=" << nonce_count << ", cnonce=\"" << cnonce << "\", response=\"" << response << "\"";
if (!opaque.empty())
os << ", opaque=\"" + opaque + "\"";
os << "\r\n";
http_request = os.str();
reset();
start_connect_();
}
catch (const std::exception& e)
{
proxy_error(Error::PROXY_NEED_CREDS, std::string("Digest Auth: ") + e.what());
}
}
std::string get_ntlm_phase_2_response()
{
for (HTTP::HeaderList::const_iterator i = http_reply.headers.begin(); i != http_reply.headers.end(); ++i)
{
const HTTP::Header& h = *i;
if (string::strcasecmp(h.name, "proxy-authenticate") == 0)
{
std::vector<std::string> v = Split::by_space<std::vector<std::string>, StandardLex, SpaceMatch, Split::NullLimit>(h.value);
if (v.size() >= 2 && string::strcasecmp("ntlm", v[0]) == 0)
return v[1];
}
}
return "";
}
void ntlm_auth_phase_1(HTTPProxy::ProxyAuthenticate& pa)
{
OPENVPN_LOG("Proxy method: NTLM" << std::endl << pa.to_string());
const std::string phase_1_reply = HTTPProxy::NTLM::phase_1();
std::ostringstream os;
gen_headers(os);
os << "Proxy-Connection: Keep-Alive\r\n";
os << "Proxy-Authorization: NTLM " << phase_1_reply << "\r\n";
http_request = os.str();
reset();
ntlm_phase_2_response_pending = true;
start_connect_();
}
void ntlm_auth_phase_2_pre()
{
// if content exists, drain it first, then progress to ntlm_auth_phase_2
const std::string content_length_str = http_reply.headers.get_value_trim("content-length");
const unsigned int content_length = parse_number_throw<unsigned int>(content_length_str, "content-length");
if (content_length)
drain_content_length = content_length;
else
ntlm_auth_phase_2();
}
void ntlm_auth_phase_2()
{
ntlm_phase_2_response_pending = false;
if (http_reply.status_code != HTTP::Status::ProxyAuthenticationRequired)
throw Exception("NTLM phase-2 status is not ProxyAuthenticationRequired");
const std::string phase_2_response = get_ntlm_phase_2_response();
if (!phase_2_response.empty())
ntlm_auth_phase_3(phase_2_response);
else
throw Exception("NTLM phase-2 response missing");
}
void ntlm_auth_phase_3(const std::string& phase_2_response)
{
// do the NTLMv2 handshake
try {
//OPENVPN_LOG("NTLM phase 3: " << phase_2_response);
const std::string phase_3_reply = HTTPProxy::NTLM::phase_3(
*config->digest_factory,
phase_2_response,
config->http_proxy_options->username,
config->http_proxy_options->password,
*config->rng);
std::ostringstream os;
gen_headers(os);
os << "Proxy-Connection: Keep-Alive\r\n";
os << "Proxy-Authorization: NTLM " << phase_3_reply << "\r\n";
http_request = os.str();
reset_partial();
http_proxy_send();
}
catch (const std::exception& e)
{
proxy_error(Error::PROXY_NEED_CREDS, std::string("NTLM Auth: ") + e.what());
}
}
void gen_headers(std::ostringstream& os)
{
bool host_header_sent = false;
// emit custom headers
{
const Options::CustomHeaderList& headers = config->http_proxy_options->headers;
for (Options::CustomHeaderList::const_iterator i = headers.begin(); i != headers.end(); ++i)
{
const Options::CustomHeader& h = **i;
if (!h.p2.empty())
{
os << h.p1 << ": " << h.p2 << "\r\n";
if (!string::strcasecmp(h.p1, "host"))
host_header_sent = true;
}
else
{
os << h.p1 << "\r\n";
const std::string h5 = h.p1.substr(0, 5);
if (!string::strcasecmp(h5, "host:"))
host_header_sent = true;
}
}
}
// emit user-agent header
{
const std::string& user_agent = config->http_proxy_options->user_agent;
if (!user_agent.empty())
os << "User-Agent: " << user_agent << "\r\n";
}
// emit host header
if (!host_header_sent)
os << "Host: " << server_host << "\r\n";
}
void stop_()
{
if (!halt)
{
halt = true;
if (impl)
impl->stop();
socket.close();
resolver.cancel();
async_resolve_cancel();
}
}
// do DNS resolve
void resolve_callback(const openvpn_io::error_code& error,
openvpn_io::ip::tcp::resolver::results_type results) override
{
if (!halt)
{
if (!error)
{
// save resolved endpoint list in proxy remote_list
proxy_remote_list().set_endpoint_range(results);
start_connect_();
}
else
{
std::ostringstream os;
os << "DNS resolve error on '" << proxy_host << "' for TCP (HTTP proxy): " << error.message();
config->stats->error(Error::RESOLVE_ERROR);
stop();
parent->transport_error(Error::UNDEF, os.str());
}
}
}
void reset()
{
stop();
halt = false;
proxy_response_limit.reset();
proxy_established = false;
reset_partial();
}
void reset_partial()
{
http_reply_status = HTTP::ReplyParser::pending;
http_reply.reset();
http_parser.reset();
ntlm_phase_2_response_pending = false;
drain_content_length = 0;
html_skip.reset();
}
// do TCP connect
void start_connect_()
{
proxy_remote_list().get_endpoint(server_endpoint);
OPENVPN_LOG("Contacting " << server_endpoint << " via HTTP Proxy");
parent->transport_wait_proxy();
socket.open(server_endpoint.protocol());
#ifdef OPENVPN_PLATFORM_TYPE_UNIX
if (config->socket_protect)
{
if (!config->socket_protect->socket_protect(socket.native_handle(), server_endpoint_addr()))
{
config->stats->error(Error::SOCKET_PROTECT_ERROR);
stop();
parent->transport_error(Error::UNDEF, "socket_protect error (HTTP Proxy)");
return;
}
}
#endif
socket.set_option(openvpn_io::ip::tcp::no_delay(true));
socket.async_connect(server_endpoint, [self=Ptr(this)](const openvpn_io::error_code& error)
{
OPENVPN_ASYNC_HANDLER;
self->start_impl_(error);
});
}
// start I/O on TCP socket
void start_impl_(const openvpn_io::error_code& error)
{
if (!halt)
{
if (!error)
{
parent->transport_wait();
impl.reset(new LinkImpl(this,
socket,
0, // send_queue_max_size is unlimited because we regulate size in cliproto.hpp
config->free_list_max_size,
(*config->frame)[Frame::READ_LINK_TCP],
config->stats));
impl->set_raw_mode(true);
impl->start();
++n_transactions;
// tell proxy to connect through to OpenVPN server
http_proxy_send();
}
else
{
proxy_remote_list().next();
std::ostringstream os;
os << "TCP connect error on '" << proxy_host << ':' << proxy_port << "' (" << server_endpoint << ") for TCP-via-HTTP-proxy session: " << error.message();
config->stats->error(Error::TCP_CONNECT_ERROR);
stop();
parent->transport_error(Error::UNDEF, os.str());
}
}
}
void http_proxy_send()
{
BufferAllocated buf;
create_http_connect_msg(buf);
send(buf);
}
// create HTTP CONNECT message
void create_http_connect_msg(BufferAllocated& buf)
{
std::ostringstream os;
const std::string& http_version = config->http_proxy_options->http_version;
os << "CONNECT " << server_host << ':' << server_port << " HTTP/";
if (!http_version.empty())
os << http_version;
else
os << "1.0";
os << "\r\n";
if (!http_request.empty())
os << http_request;
else
gen_headers(os);
os << "\r\n";
const std::string str = os.str();
http_request = "";
OPENVPN_LOG_NTNL("TO PROXY: " << str);
config->frame->prepare(Frame::WRITE_HTTP, buf);
buf_write_string(buf, str);
}
RemoteList& remote_list() const { return *config->remote_list; }
RemoteList& proxy_remote_list() const { return *config->http_proxy_options->proxy_server; }
std::string proxy_host;
std::string proxy_port;
std::string server_host;
std::string server_port;
openvpn_io::ip::tcp::socket socket;
ClientConfig::Ptr config;
TransportClientParent* parent;
LinkImpl::Ptr impl;
openvpn_io::ip::tcp::resolver resolver;
LinkImpl::protocol::endpoint server_endpoint;
bool halt;
unsigned int n_transactions;
ProxyResponseLimit proxy_response_limit;
bool proxy_established;
HTTP::ReplyParser::status http_reply_status;
HTTP::Reply http_reply;
HTTP::ReplyParser http_parser;
std::string http_request;
bool ntlm_phase_2_response_pending;
size_t drain_content_length;
std::unique_ptr<HTTP::HTMLSkip> html_skip;
};
inline TransportClient::Ptr ClientConfig::new_transport_client_obj(openvpn_io::io_context& io_context, TransportClientParent* parent)
{
return TransportClient::Ptr(new Client(io_context, this, parent));
}
}
} // namespace openvpn
#endif