Added TLS support for API and many other TLS related changes.

This commit is contained in:
XMRig 2020-03-18 20:09:11 +07:00
parent 92a258f142
commit 5b610e4dfe
No known key found for this signature in database
GPG key ID: 446A53638BE94409
38 changed files with 1601 additions and 178 deletions

View file

@ -27,7 +27,7 @@
#ifdef XMRIG_FEATURE_TLS
# include "base/net/http/HttpsClient.h"
# include "base/net/https/HttpsClient.h"
#endif

View file

@ -29,7 +29,6 @@
#include "base/io/log/Log.h"
#include "base/kernel/Platform.h"
#include "base/net/dns/Dns.h"
#include "base/tools/Baton.h"
#include <sstream>
@ -41,31 +40,6 @@ namespace xmrig {
static const char *kCRLF = "\r\n";
class HttpClientWriteBaton : public Baton<uv_write_t>
{
public:
inline HttpClientWriteBaton(const std::string &header, std::string &&body) :
m_body(std::move(body)),
m_header(header)
{
m_bufs[0] = uv_buf_init(const_cast<char *>(m_header.c_str()), m_header.size());
m_bufs[1] = m_body.empty() ? uv_buf_init(nullptr, 0) : uv_buf_init(const_cast<char *>(m_body.c_str()), m_body.size());
}
void write(uv_stream_t *stream)
{
uv_write(&req, stream, m_bufs, nbufs(), [](uv_write_t *req, int) { delete reinterpret_cast<HttpClientWriteBaton *>(req->data); });
}
private:
inline size_t nbufs() const { return m_bufs[1].len > 0 ? 2 : 1; }
std::string m_body;
std::string m_header;
uv_buf_t m_bufs[2]{};
};
} // namespace xmrig
@ -137,7 +111,8 @@ void xmrig::HttpClient::handshake()
headers.clear();
write(ss.str());
body.insert(0, ss.str());
write(std::move(body), false);
}
@ -149,13 +124,6 @@ void xmrig::HttpClient::read(const char *data, size_t size)
}
void xmrig::HttpClient::write(const std::string &header)
{
auto baton = new HttpClientWriteBaton(header, std::move(body));
baton->write(stream());
}
void xmrig::HttpClient::onConnect(uv_connect_t *req, int status)
{
auto client = static_cast<HttpClient *>(req->data);

View file

@ -59,7 +59,6 @@ protected:
virtual void handshake();
virtual void read(const char *data, size_t size);
virtual void write(const std::string &header);
protected:
inline const FetchRequest &req() const { return m_req; }

View file

@ -27,6 +27,7 @@
#include "base/net/http/HttpContext.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/kernel/interfaces/IHttpListener.h"
#include "base/tools/Baton.h"
#include "base/tools/Chrono.h"
@ -42,6 +43,37 @@ static std::map<uint64_t, HttpContext *> storage;
static uint64_t SEQUENCE = 0;
class HttpWriteBaton : public Baton<uv_write_t>
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpWriteBaton)
inline HttpWriteBaton(std::string &&body, HttpContext *ctx) :
m_ctx(ctx),
m_body(std::move(body))
{
m_buf = uv_buf_init(const_cast<char *>(m_body.c_str()), m_body.size());
}
inline ~HttpWriteBaton()
{
if (m_ctx) {
m_ctx->close();
}
}
void write(uv_stream_t *stream)
{
uv_write(&req, stream, &m_buf, 1, [](uv_write_t *req, int) { delete reinterpret_cast<HttpWriteBaton *>(req->data); });
}
private:
HttpContext *m_ctx;
std::string m_body;
uv_buf_t m_buf{};
};
} // namespace xmrig
@ -75,6 +107,17 @@ xmrig::HttpContext::~HttpContext()
}
void xmrig::HttpContext::write(std::string &&data, bool close)
{
if (uv_is_writable(stream()) != 1) {
return;
}
auto baton = new HttpWriteBaton(std::move(data), close ? this : nullptr);
baton->write(stream());
}
bool xmrig::HttpContext::isRequest() const
{
return m_parser->type == HTTP_REQUEST;

View file

@ -65,6 +65,8 @@ public:
inline const char *tlsVersion() const override { return nullptr; }
inline uint16_t port() const override { return 0; }
void write(std::string &&data, bool close) override;
bool isRequest() const override;
size_t parse(const char *data, size_t size);
std::string ip() const override;

View file

@ -53,12 +53,13 @@ public:
inline uint64_t id() const { return m_id; }
virtual bool isRequest() const = 0;
virtual const char *host() const = 0;
virtual const char *tlsFingerprint() const = 0;
virtual const char *tlsVersion() const = 0;
virtual std::string ip() const = 0;
virtual uint16_t port() const = 0;
virtual bool isRequest() const = 0;
virtual const char *host() const = 0;
virtual const char *tlsFingerprint() const = 0;
virtual const char *tlsVersion() const = 0;
virtual std::string ip() const = 0;
virtual uint16_t port() const = 0;
virtual void write(std::string &&data, bool close) = 0;
int method = 0;
int status = 0;

View file

@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -28,8 +28,6 @@
#include "3rdparty/http-parser/http_parser.h"
#include "base/io/log/Log.h"
#include "base/net/http/HttpContext.h"
#include "base/tools/Baton.h"
#include "base/tools/Object.h"
#include <cinttypes>
@ -45,52 +43,6 @@ static const char *kCRLF = "\r\n";
static const char *kUserAgent = "user-agent";
class WriteBaton : public Baton<uv_write_t>
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(WriteBaton)
inline WriteBaton(const std::stringstream &ss, const char *data, size_t size, HttpContext *ctx) :
m_ctx(ctx),
m_header(ss.str())
{
bufs[0].len = m_header.size();
bufs[0].base = const_cast<char *>(m_header.c_str());
if (data) {
bufs[1].len = size;
bufs[1].base = new char[size];
memcpy(bufs[1].base, data, size);
}
else {
bufs[1].base = nullptr;
bufs[1].len = 0;
}
}
inline ~WriteBaton()
{
if (count() == 2) {
delete [] bufs[1].base;
}
m_ctx->close();
}
inline size_t count() const { return bufs[1].base == nullptr ? 1 : 2; }
inline size_t size() const { return bufs[0].len + bufs[1].len; }
inline static void onWrite(uv_write_t *req, int) { delete reinterpret_cast<WriteBaton *>(req->data); }
uv_buf_t bufs[2]{};
private:
HttpContext *m_ctx;
std::string m_header;
};
} // namespace xmrig
@ -134,8 +86,8 @@ void xmrig::HttpResponse::end(const char *data, size_t size)
ss << kCRLF;
auto ctx = HttpContext::get(m_id);
auto baton = new WriteBaton(ss, data, size, ctx);
auto ctx = HttpContext::get(m_id);
std::string body = data ? (ss.str() + std::string(data, size)) : ss.str();
# ifndef APP_DEBUG
if (statusCode() >= 400)
@ -149,11 +101,11 @@ void xmrig::HttpResponse::end(const char *data, size_t size)
ctx->url.c_str(),
err ? 31 : 32,
statusCode(),
baton->size(),
body.size(),
ctx->elapsed(),
ctx->headers.count(kUserAgent) ? ctx->headers.at(kUserAgent).c_str() : nullptr
);
}
uv_write(&baton->req, ctx->stream(), baton->bufs, baton->count(), WriteBaton::onWrite);
ctx->write(std::move(body), true);
}

View file

@ -6,8 +6,8 @@
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2019 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2019 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by

View file

@ -28,11 +28,11 @@
#include <uv.h>
#include "base/net/http/HttpServer.h"
#include "3rdparty/http-parser/http_parser.h"
#include "base/kernel/interfaces/IHttpListener.h"
#include "base/net/http/HttpContext.h"
#include "base/net/http/HttpResponse.h"
#include "base/net/http/HttpServer.h"
xmrig::HttpServer::HttpServer(const std::shared_ptr<IHttpListener> &listener) :
@ -56,12 +56,7 @@ void xmrig::HttpServer::onConnection(uv_stream_t *stream, uint16_t)
[](uv_handle_t *, size_t suggested_size, uv_buf_t *buf)
{
buf->base = new char[suggested_size];
# ifdef _WIN32
buf->len = static_cast<unsigned int>(suggested_size);
# else
buf->len = suggested_size;
# endif
buf->len = suggested_size;
},
[](uv_stream_t *tcp, ssize_t nread, const uv_buf_t *buf)
{

View file

@ -1,204 +0,0 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <openssl/ssl.h>
#include <uv.h>
#include "base/net/http/HttpsClient.h"
#include "base/io/log/Log.h"
#include "base/tools/Buffer.h"
#ifdef _MSC_VER
# define strncasecmp(x,y,z) _strnicmp(x,y,z)
#endif
xmrig::HttpsClient::HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener) :
HttpClient(std::move(req), listener)
{
m_ctx = SSL_CTX_new(SSLv23_method());
assert(m_ctx != nullptr);
if (!m_ctx) {
return;
}
m_writeBio = BIO_new(BIO_s_mem());
m_readBio = BIO_new(BIO_s_mem());
SSL_CTX_set_options(m_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
}
xmrig::HttpsClient::~HttpsClient()
{
if (m_ctx) {
SSL_CTX_free(m_ctx);
}
if (m_ssl) {
SSL_free(m_ssl);
}
}
const char *xmrig::HttpsClient::tlsFingerprint() const
{
return m_ready ? m_fingerprint : nullptr;
}
const char *xmrig::HttpsClient::tlsVersion() const
{
return m_ready ? SSL_get_version(m_ssl) : nullptr;
}
void xmrig::HttpsClient::handshake()
{
m_ssl = SSL_new(m_ctx);
assert(m_ssl != nullptr);
if (!m_ssl) {
return;
}
SSL_set_connect_state(m_ssl);
SSL_set_bio(m_ssl, m_readBio, m_writeBio);
SSL_set_tlsext_host_name(m_ssl, host());
SSL_do_handshake(m_ssl);
flush();
}
void xmrig::HttpsClient::read(const char *data, size_t size)
{
BIO_write(m_readBio, data, size);
if (!SSL_is_init_finished(m_ssl)) {
const int rc = SSL_connect(m_ssl);
if (rc < 0 && SSL_get_error(m_ssl, rc) == SSL_ERROR_WANT_READ) {
flush();
} else if (rc == 1) {
X509 *cert = SSL_get_peer_certificate(m_ssl);
if (!verify(cert)) {
X509_free(cert);
return close(UV_EPROTO);
}
X509_free(cert);
m_ready = true;
HttpClient::handshake();
}
return;
}
int bytes_read = 0;
while ((bytes_read = SSL_read(m_ssl, m_buf, sizeof(m_buf))) > 0) {
HttpClient::read(m_buf, static_cast<size_t>(bytes_read));
}
}
void xmrig::HttpsClient::write(const std::string &header)
{
SSL_write(m_ssl, (header + body).c_str(), header.size() + body.size());
body.clear();
flush();
}
bool xmrig::HttpsClient::verify(X509 *cert)
{
if (cert == nullptr) {
return false;
}
if (!verifyFingerprint(cert)) {
if (!isQuiet()) {
LOG_ERR("[%s:%d] Failed to verify server certificate fingerprint", host(), port());
if (strlen(m_fingerprint) == 64 && !req().fingerprint.isNull()) {
LOG_ERR("\"%s\" was given", m_fingerprint);
LOG_ERR("\"%s\" was configured", req().fingerprint.data());
}
}
return false;
}
return true;
}
bool xmrig::HttpsClient::verifyFingerprint(X509 *cert)
{
const EVP_MD *digest = EVP_get_digestbyname("sha256");
if (digest == nullptr) {
return false;
}
unsigned char md[EVP_MAX_MD_SIZE];
unsigned int dlen;
if (X509_digest(cert, digest, md, &dlen) != 1) {
return false;
}
Buffer::toHex(md, 32, m_fingerprint);
return req().fingerprint.isNull() || strncasecmp(m_fingerprint, req().fingerprint.data(), 64) == 0;
}
void xmrig::HttpsClient::flush()
{
uv_buf_t buf;
buf.len = BIO_get_mem_data(m_writeBio, &buf.base);
if (buf.len == 0) {
return;
}
bool result = false;
if (uv_is_writable(stream())) {
result = uv_try_write(stream(), &buf, 1) == static_cast<int>(buf.len);
if (!result) {
close(UV_EIO);
}
}
(void) BIO_reset(m_writeBio);
}

View file

@ -1,78 +0,0 @@
/* XMRig
* Copyright 2010 Jeff Garzik <jgarzik@pobox.com>
* Copyright 2012-2014 pooler <pooler@litecoinpool.org>
* Copyright 2014 Lucas Jones <https://github.com/lucasjones>
* Copyright 2014-2016 Wolf9466 <https://github.com/OhGodAPet>
* Copyright 2016 Jay D Dee <jayddee246@gmail.com>
* Copyright 2017-2018 XMR-Stak <https://github.com/fireice-uk>, <https://github.com/psychocrypt>
* Copyright 2014-2019 heapwolf <https://github.com/heapwolf>
* Copyright 2018-2020 SChernykh <https://github.com/SChernykh>
* Copyright 2016-2020 XMRig <https://github.com/xmrig>, <support@xmrig.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef XMRIG_HTTPSCLIENT_H
#define XMRIG_HTTPSCLIENT_H
using BIO = struct bio_st;
using SSL_CTX = struct ssl_ctx_st;
using SSL = struct ssl_st;
using X509 = struct x509_st;
#include "base/net/http/HttpClient.h"
#include "base/tools/String.h"
namespace xmrig {
class HttpsClient : public HttpClient
{
public:
XMRIG_DISABLE_COPY_MOVE_DEFAULT(HttpsClient)
HttpsClient(FetchRequest &&req, const std::weak_ptr<IHttpListener> &listener);
~HttpsClient() override;
const char *tlsFingerprint() const override;
const char *tlsVersion() const override;
protected:
void handshake() override;
void read(const char *data, size_t size) override;
void write(const std::string &header) override;
private:
bool verify(X509 *cert);
bool verifyFingerprint(X509 *cert);
void flush();
BIO *m_readBio = nullptr;
BIO *m_writeBio = nullptr;
bool m_ready = false;
char m_buf[1024 * 2]{};
char m_fingerprint[32 * 2 + 8]{};
SSL *m_ssl = nullptr;
SSL_CTX *m_ctx = nullptr;
};
} // namespace xmrig
#endif // XMRIG_HTTPSCLIENT_H