Ai
1 Star 0 Fork 0

wangcichen/urbackup_backend

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
OpenSSLPipe.cpp 10.43 KB
一键复制 编辑 原始数据 按行查看 历史
Martin 提交于 2020-10-07 00:29 +08:00 . Fix build issue
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
#ifdef WITH_OPENSSL
#include "OpenSSLPipe.h"
#include <openssl/ssl.h>
#include <openssl/conf.h>
#include <openssl/err.h>
#include <openssl/x509v3.h>
#include <openssl/bio.h>
#include "Server.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifndef _WIN32
#include <unistd.h>
#endif
OpenSSLPipe::OpenSSLPipe(CStreamPipe * bpipe)
: bpipe(bpipe), bbio(NULL), ctx(NULL),
has_error(false)
{
}
OpenSSLPipe::~OpenSSLPipe()
{
if (bbio != NULL)
BIO_free_all(bbio);
if (ctx != NULL)
SSL_CTX_free(ctx);
}
namespace
{
void log_ssl_err()
{
unsigned long err = ERR_get_error();
if (err != 0)
{
const char* const str = ERR_reason_error_string(err);
if (str)
{
Server->Log(std::string("OpenSSL error: ") + str, LL_WARNING);
}
}
}
const char* const PREFERRED_CIPHERS = "HIGH:!aNULL:!kRSA:!SRP:!PSK:!CAMELLIA:!RC4:!MD5:!DSS";
//ALL that shit just to set MSG_NOSIGNAL :(
BIO_METHOD* meth_socket_nosigpipe;
int bio_read(BIO *b, char * buf, int len)
{
if (buf == NULL || len <= 0)
return 0;
BIO_clear_retry_flags(b);
int fd = BIO_get_fd(b, NULL);
int rc = recv(fd, buf, len, MSG_NOSIGNAL);
if (rc <= 0)
{
#ifdef _WIN32
DWORD err = WSAGetLastError();
if (err == WSAEWOULDBLOCK || err == WSAEINTR)
{
BIO_set_retry_read(b);
}
#else
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
{
BIO_set_retry_read(b);
}
#endif
}
return rc;
}
int bio_read_ex(BIO *b, char * buf, size_t len, size_t *readbytes)
{
int rc = bio_read(b, buf, static_cast<int>(len));
if (rc > 0)
{
*readbytes = rc;
return 1;
}
else
{
return 0;
}
}
int bio_write(BIO* b, const char* buf, int len)
{
if (buf == NULL || len <= 0)
return 0;
BIO_clear_retry_flags(b);
int fd = BIO_get_fd(b, NULL);
int rc = send(fd, buf, len, MSG_NOSIGNAL);
if(rc<0)
{
#ifdef _WIN32
DWORD err = WSAGetLastError();
if (err == EINTR || err== WSAEWOULDBLOCK)
{
BIO_set_retry_write(b);
}
#else
if (errno == EINTR || errno == EAGAIN || errno== EWOULDBLOCK)
{
BIO_set_retry_write(b);
}
#endif
return rc;
}
return rc;
}
int bio_write_ex(BIO* b, const char* buf, size_t len, size_t *written)
{
int rc = bio_write(b, buf, static_cast<int>(len));
if (rc > 0)
{
*written = rc;
return 1;
}
else
{
return 0;
}
}
}
void OpenSSLPipe::init()
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
SSL_library_init();
SSL_load_error_strings();
OPENSSL_config(NULL);
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
const BIO_METHOD* smeth = BIO_s_socket();
#else
BIO_METHOD* smeth = (BIO_METHOD*)BIO_s_socket();
#endif
int s_socket_nosignpipe = BIO_get_new_index();
meth_socket_nosigpipe = BIO_meth_new(s_socket_nosignpipe, "socket_nosigpipe");
BIO_meth_set_callback_ctrl(meth_socket_nosigpipe, BIO_meth_get_callback_ctrl(smeth));
BIO_meth_set_create(meth_socket_nosigpipe, BIO_meth_get_create(smeth));
BIO_meth_set_ctrl(meth_socket_nosigpipe, BIO_meth_get_ctrl(smeth));
BIO_meth_set_destroy(meth_socket_nosigpipe, BIO_meth_get_destroy(smeth));
BIO_meth_set_gets(meth_socket_nosigpipe, BIO_meth_get_gets(smeth));
BIO_meth_set_puts(meth_socket_nosigpipe, BIO_meth_get_puts(smeth));
BIO_meth_set_read(meth_socket_nosigpipe, bio_read);
BIO_meth_set_write(meth_socket_nosigpipe, bio_write);
#if OPENSSL_VERSION_NUMBER >= 0x10101000L
BIO_meth_set_read_ex(meth_socket_nosigpipe, bio_read_ex);
BIO_meth_set_write_ex(meth_socket_nosigpipe, bio_write_ex);
#endif
}
#ifndef S_ISDIR
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
bool OpenSSLPipe::ssl_connect(const std::string & p_hostname, int timeoutms)
{
const SSL_METHOD* method = SSLv23_method();
if (method == NULL)
{
log_ssl_err();
return false;
}
ctx = SSL_CTX_new(method);
if (ctx == NULL)
{
log_ssl_err();
return false;
}
SSL_CTX_set_verify_depth(ctx, 5);
const long flags = SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION;
SSL_CTX_set_options(ctx, flags);
std::string capath = Server->getServerParameter("capath");
if (!capath.empty())
{
struct stat sbuf;
if (stat(capath.c_str(), &sbuf)==0 &&
!S_ISDIR(sbuf.st_mode))
{
long res = SSL_CTX_load_verify_locations(ctx, capath.c_str(), NULL);
if (res != 1)
{
log_ssl_err();
return false;
}
}
else
{
long res = SSL_CTX_load_verify_locations(ctx, NULL, capath.c_str());
if (res != 1)
{
log_ssl_err();
return false;
}
}
}
else
{
#ifndef OPENSSL_SEARCH_CA
long res = SSL_CTX_set_default_verify_paths(ctx);
if (res != 1)
{
log_ssl_err();
return false;
}
#else
char* capaths[] = {
"/etc/pki/tls/certs/ca-bundle.crt",
"/etc/ssl/certs/ca-certificates.crt",
"/var/lib/ca-certificates/ca-bundle.pem",
"/system/etc/security/cacerts",
"/etc/ssl/certs",
"/etc/pki/tls/certs",
"/etc/openssl/certs",
"/var/ssl/certs",
NULL
};
bool found_ca = false;
for (size_t i=0;capaths[i]!=NULL;++i)
{
char* cafn = capaths[i];
struct stat sbuf;
if (stat(cafn, &sbuf) == 0)
{
if (S_ISDIR(sbuf.st_mode))
{
long res = SSL_CTX_load_verify_locations(ctx, NULL, cafn);
if (res == 1)
{
found_ca = true;
break;
}
}
else
{
long res = SSL_CTX_load_verify_locations(ctx, cafn, NULL);
if (res == 1)
{
found_ca = true;
break;
}
}
}
}
if (!found_ca)
{
Server->Log("No ca certificates found. Please specify certificate bundle path via capath parameter", LL_WARNING);
return false;
}
#endif
}
bbio = BIO_new_ssl(ctx, 1);
if (bbio == NULL)
{
log_ssl_err();
return false;
}
BIO* sbio = BIO_new(meth_socket_nosigpipe);
if (sbio == NULL)
{
log_ssl_err();
return false;
}
BIO_set_fd(sbio, static_cast<int>(bpipe->getSocket()), BIO_NOCLOSE);
BIO_push(bbio, sbio);
BIO_set_nbio(bbio, 1);
SSL *ssl = NULL;
BIO_get_ssl(bbio, &ssl);
if (ssl == NULL)
{
log_ssl_err();
return false;
}
long res = SSL_set_cipher_list(ssl, PREFERRED_CIPHERS);
if (res!=1)
{
log_ssl_err();
return false;
}
res = SSL_set_tlsext_host_name(ssl, p_hostname.c_str());
if (res != 1)
{
log_ssl_err();
return false;
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L
X509_VERIFY_PARAM* param = SSL_get0_param(ssl);
X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
res = X509_VERIFY_PARAM_set1_host(param, p_hostname.c_str(), p_hostname.size());
#else
SSL_set_hostflags(ssl, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
res = SSL_set1_host(ssl, p_hostname.c_str());
#endif
if (res != 1)
{
log_ssl_err();
return false;
}
SSL_set_verify(ssl, SSL_VERIFY_PEER, NULL);
int64 starttime = Server->getTimeMS();
do
{
res = BIO_do_handshake(bbio);
if (res != 1
&& !BIO_should_retry(bbio) )
{
log_ssl_err();
return false;
}
if (res == 1)
break;
int64 rtime = timeoutms - (Server->getTimeMS() - starttime);
if (rtime < 0)
rtime = 0;
if (timeoutms < 0)
rtime = -1;
if (!bpipe->isReadOrWritable(static_cast<int>(rtime)))
break;
} while (Server->getTimeMS() - starttime < timeoutms);
if (res != 1)
{
Server->Log("SSL connect timeout", LL_WARNING);
return false;
}
X509* cert = SSL_get_peer_certificate(ssl);
if (cert)
{
X509_free(cert);
}
if (cert == NULL)
{
Server->Log("Getting server certificate failed", LL_WARNING);
return false;
}
res = SSL_get_verify_result(ssl);
if (res != X509_V_OK)
{
const char* const str = ERR_reason_error_string(res);
if (str)
{
Server->Log("Verifying certificate of hostname " + p_hostname + " failed with OpenSSL error code " + str, LL_WARNING);
}
return false;
}
return true;
}
size_t OpenSSLPipe::Read(char * buffer, size_t bsize, int timeoutms)
{
bool retry=false;
do
{
if (BIO_pending(bbio)<=0)
{
if (!bpipe->isReadable(timeoutms))
{
return 0;
}
}
int rc = BIO_read(bbio, buffer, static_cast<int>(bsize));
if (rc <= 0)
{
if (!BIO_should_retry(bbio))
{
has_error = true;
return 0;
}
else
{
retry=true;
}
}
else
{
bpipe->doThrottle(rc, false, true);
return rc;
}
} while(retry);
return 0;
}
bool OpenSSLPipe::Write(const char * buffer, size_t bsize, int timeoutms, bool flush)
{
if (bsize == 0)
return true;
if (!bpipe->isWritable(timeoutms))
{
return false;
}
int rc = BIO_write(bbio, buffer, static_cast<int>(bsize));
if (rc <= 0)
{
if (!BIO_should_retry(bbio))
{
has_error = true;
}
return false;
}
else
{
if (rc < bsize)
{
bpipe->doThrottle(rc, true, true);
return Write(buffer + rc, bsize - rc, -1, flush);
}
return true;
}
}
size_t OpenSSLPipe::Read(std::string * ret, int timeoutms)
{
char buffer[8192];
size_t l = Read(buffer, 8192, timeoutms);
if (l>0)
{
ret->assign(buffer, l);
}
else
{
return 0;
}
return l;
}
bool OpenSSLPipe::Write(const std::string & str, int timeoutms, bool flush)
{
return Write(&str[0], str.size(), timeoutms, flush);
}
bool OpenSSLPipe::Flush(int timeoutms)
{
return bpipe->Flush(timeoutms);
}
bool OpenSSLPipe::isWritable(int timeoutms)
{
return bpipe->isWritable(timeoutms);
}
bool OpenSSLPipe::isReadable(int timeoutms)
{
return BIO_pending(bbio)>0 || bpipe->isReadable(timeoutms);
}
bool OpenSSLPipe::hasError(void)
{
return has_error || bpipe->hasError();
}
void OpenSSLPipe::shutdown(void)
{
bpipe->shutdown();
}
size_t OpenSSLPipe::getNumWaiters()
{
return bpipe->getNumWaiters();
}
size_t OpenSSLPipe::getNumElements(void)
{
return bpipe->getNumElements();
}
void OpenSSLPipe::addThrottler(IPipeThrottler * throttler)
{
bpipe->addThrottler(throttler);
}
void OpenSSLPipe::addOutgoingThrottler(IPipeThrottler * throttler)
{
bpipe->addOutgoingThrottler(throttler);
}
void OpenSSLPipe::addIncomingThrottler(IPipeThrottler * throttler)
{
bpipe->addIncomingThrottler(throttler);
}
_i64 OpenSSLPipe::getTransferedBytes(void)
{
return bpipe->getTransferedBytes();
}
void OpenSSLPipe::resetTransferedBytes(void)
{
bpipe->resetTransferedBytes();
}
#endif //WITH_OPENSSL
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/wangcichen/urbackup_backend.git
git@gitee.com:wangcichen/urbackup_backend.git
wangcichen
urbackup_backend
urbackup_backend
dev

搜索帮助