From 0f3afed6640cc545b4db09b6318617bd5cc4facd Mon Sep 17 00:00:00 2001 From: Povilas Kanapickas Date: Mon, 1 Nov 2021 04:29:54 +0200 Subject: [PATCH] gui: Switch SSL certificate handler to barrier::fs paths --- src/gui/src/SslCertificate.cpp | 47 ++++++++++------------------------ src/gui/src/SslCertificate.h | 11 +++----- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/src/gui/src/SslCertificate.cpp b/src/gui/src/SslCertificate.cpp index 910e0a8d..26f3b3ee 100644 --- a/src/gui/src/SslCertificate.cpp +++ b/src/gui/src/SslCertificate.cpp @@ -22,41 +22,32 @@ #include "net/FingerprintDatabase.h" #include "net/SecureUtils.h" -#include -#include -#include - #include #include #include #include #include -static const char kCertificateFilename[] = "Barrier.pem"; -static const char kSslDir[] = "SSL"; - SslCertificate::SslCertificate(QObject *parent) : QObject(parent) { - m_ProfileDir = barrier::DataDirectories::profile(); - if (m_ProfileDir.empty()) { + if (barrier::DataDirectories::profile().empty()) { emit error(tr("Failed to get profile directory.")); } } void SslCertificate::generateCertificate() { - auto cert_path = getCertificatePath(); - - QFile file(QString::fromStdString(cert_path)); - if (!file.exists() || !isCertificateValid(cert_path)) { - QDir sslDir(QString::fromStdString(getCertificateDirectory())); - if (!sslDir.exists()) { - sslDir.mkpath("."); - } + auto cert_path = barrier::DataDirectories::ssl_certificate_path(); + if (!barrier::fs::exists(cert_path) || !is_certificate_valid(cert_path)) { try { - barrier::generate_pem_self_signed_cert(cert_path); + auto cert_dir = cert_path.parent_path(); + if (!barrier::fs::exists(cert_dir)) { + barrier::fs::create_directories(cert_dir); + } + + barrier::generate_pem_self_signed_cert(cert_path.u8string()); } catch (const std::exception& e) { emit error(QString("SSL tool failed: %1").arg(e.what())); return; @@ -65,19 +56,19 @@ void SslCertificate::generateCertificate() emit info(tr("SSL certificate generated.")); } - generateFingerprint(cert_path); + generate_fingerprint(cert_path); emit generateFinished(); } -void SslCertificate::generateFingerprint(const std::string& cert_path) +void SslCertificate::generate_fingerprint(const barrier::fs::path& cert_path) { try { auto local_path = barrier::DataDirectories::local_ssl_fingerprints_path(); barrier::FingerprintDatabase db; - db.add_trusted(barrier::get_pem_file_cert_fingerprint(cert_path, + db.add_trusted(barrier::get_pem_file_cert_fingerprint(cert_path.u8string(), barrier::FingerprintType::SHA1)); - db.add_trusted(barrier::get_pem_file_cert_fingerprint(cert_path, + db.add_trusted(barrier::get_pem_file_cert_fingerprint(cert_path.u8string(), barrier::FingerprintType::SHA256)); db.write(local_path); @@ -87,17 +78,7 @@ void SslCertificate::generateFingerprint(const std::string& cert_path) } } -std::string SslCertificate::getCertificatePath() -{ - return getCertificateDirectory() + QDir::separator().toLatin1() + kCertificateFilename; -} - -std::string SslCertificate::getCertificateDirectory() -{ - return m_ProfileDir + QDir::separator().toLatin1() + kSslDir; -} - -bool SslCertificate::isCertificateValid(const std::string& path) +bool SslCertificate::is_certificate_valid(const barrier::fs::path& path) { OpenSSL_add_all_algorithms(); ERR_load_crypto_strings(); diff --git a/src/gui/src/SslCertificate.h b/src/gui/src/SslCertificate.h index 7f77771a..eae7fd6c 100644 --- a/src/gui/src/SslCertificate.h +++ b/src/gui/src/SslCertificate.h @@ -19,6 +19,7 @@ #include #include +#include "io/filesystem.h" class SslCertificate : public QObject { @@ -36,13 +37,7 @@ signals: void generateFinished(); private: - std::pair runTool(const QStringList& args); - void generateFingerprint(const std::string& cert_path); + void generate_fingerprint(const barrier::fs::path& cert_path); - std::string getCertificatePath(); - std::string getCertificateDirectory(); - - bool isCertificateValid(const std::string& path); -private: - std::string m_ProfileDir; + bool is_certificate_valid(const barrier::fs::path& path); };