refactor: SecureSocket::loadCertificates(const std::string) & => SecureSocket::loadCertificate(const QString &)
This commit is contained in:
parent
e5513101a6
commit
7d07222832
3 changed files with 17 additions and 29 deletions
|
|
@ -40,8 +40,7 @@ std::unique_ptr<IDataSocket> SecureListenSocket::accept()
|
||||||
setListeningJob();
|
setListeningJob();
|
||||||
|
|
||||||
// default location of the TLS cert file in users dir
|
// default location of the TLS cert file in users dir
|
||||||
if (const auto certificateFilename = Settings::value(Settings::Security::Certificate).toString().toStdString();
|
if (!secureSocket->loadCertificate(Settings::value(Settings::Security::Certificate).toString())) {
|
||||||
!secureSocket->loadCertificates(certificateFilename)) {
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@
|
||||||
#include "arch/ArchException.h"
|
#include "arch/ArchException.h"
|
||||||
#include "base/IEventQueue.h"
|
#include "base/IEventQueue.h"
|
||||||
#include "base/Log.h"
|
#include "base/Log.h"
|
||||||
#include "base/Path.h"
|
|
||||||
#include "common/Settings.h"
|
#include "common/Settings.h"
|
||||||
#include "mt/Lock.h"
|
#include "mt/Lock.h"
|
||||||
#include "net/FingerprintDatabase.h"
|
#include "net/FingerprintDatabase.h"
|
||||||
|
|
@ -21,7 +20,6 @@
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
|
|
@ -288,41 +286,35 @@ void SecureSocket::initSsl(bool server)
|
||||||
initContext(server);
|
initContext(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SecureSocket::loadCertificates(const std::string &filename)
|
bool SecureSocket::loadCertificate(const QString &filename)
|
||||||
{
|
{
|
||||||
std::scoped_lock ssl_lock{ssl_mutex_};
|
std::scoped_lock ssl_lock{ssl_mutex_};
|
||||||
|
|
||||||
if (filename.empty()) {
|
if (filename.isEmpty()) {
|
||||||
SslLogger::logError("tls certificate is not specified");
|
SslLogger::logError("tls certificate is not specified");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
|
||||||
std::ifstream file(deskflow::filesystem::path(filename));
|
|
||||||
bool exist = file.good();
|
|
||||||
file.close();
|
|
||||||
|
|
||||||
if (!exist) {
|
|
||||||
std::string errorMsg("tls certificate doesn't exist: ");
|
|
||||||
errorMsg.append(filename);
|
|
||||||
SslLogger::logError(errorMsg.c_str());
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int r = 0;
|
if (!QFile::exists(filename)) {
|
||||||
r = SSL_CTX_use_certificate_file(m_ssl->m_context, filename.c_str(), SSL_FILETYPE_PEM);
|
std::string errorMsg("tls certificate doesn't exist: ");
|
||||||
if (r <= 0) {
|
errorMsg.append(filename.toStdString());
|
||||||
|
SslLogger::logError(errorMsg.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto fName = filename.toStdString();
|
||||||
|
|
||||||
|
if (SSL_CTX_use_certificate_file(m_ssl->m_context, fName.c_str(), SSL_FILETYPE_PEM) <= 0) {
|
||||||
SslLogger::logError("could not use tls certificate");
|
SslLogger::logError("could not use tls certificate");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = SSL_CTX_use_PrivateKey_file(m_ssl->m_context, filename.c_str(), SSL_FILETYPE_PEM);
|
if (SSL_CTX_use_PrivateKey_file(m_ssl->m_context, fName.c_str(), SSL_FILETYPE_PEM) <= 0) {
|
||||||
if (r <= 0) {
|
|
||||||
SslLogger::logError("could not use tls private key");
|
SslLogger::logError("could not use tls private key");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = SSL_CTX_check_private_key(m_ssl->m_context);
|
if (!SSL_CTX_check_private_key(m_ssl->m_context)) {
|
||||||
if (!r) {
|
|
||||||
SslLogger::logError("could not verify tls private key");
|
SslLogger::logError("could not verify tls private key");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -461,10 +453,7 @@ int SecureSocket::secureAccept(int socket)
|
||||||
|
|
||||||
int SecureSocket::secureConnect(int socket)
|
int SecureSocket::secureConnect(int socket)
|
||||||
{
|
{
|
||||||
|
if (!loadCertificate(Settings::value(Settings::Security::Certificate).toString())) {
|
||||||
std::string certDir = Settings::value(Settings::Security::Certificate).toString().toStdString();
|
|
||||||
|
|
||||||
if (!loadCertificates(certDir)) {
|
|
||||||
LOG_ERR("could not load client certificates");
|
LOG_ERR("could not load client certificates");
|
||||||
disconnect();
|
disconnect();
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ public:
|
||||||
JobResult doRead() override;
|
JobResult doRead() override;
|
||||||
JobResult doWrite() override;
|
JobResult doWrite() override;
|
||||||
void initSsl(bool server);
|
void initSsl(bool server);
|
||||||
bool loadCertificates(const std::string &CertFile);
|
bool loadCertificate(const QString &filename);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// SSL
|
// SSL
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue