From 1a485045f673a8555b1e74f702b14e7ae0cc9cc6 Mon Sep 17 00:00:00 2001
From: Tobias Triffterer <tobias@ep1.ruhr-uni-bochum.de>
Date: Mon, 3 May 2021 22:50:10 +0200
Subject: [PATCH] Wrapper Class for Certificate Resources

This class pulls all CA certificate files from the Qt resource system
after making sure these resources are properly initialized, which is
required as they are part of a static library.
---
 src/cacertificates.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++
 src/cacertificates.h   | 51 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 104 insertions(+)
 create mode 100644 src/cacertificates.cpp
 create mode 100644 src/cacertificates.h

diff --git a/src/cacertificates.cpp b/src/cacertificates.cpp
new file mode 100644
index 0000000..ee25f6a
--- /dev/null
+++ b/src/cacertificates.cpp
@@ -0,0 +1,53 @@
+/**
+ * @file cacertificates.h
+ *
+ * @author Tobias Triffterer
+ *
+ * @brief Retrieve Certificates from Qt Resources
+ *
+ * Rutherford Experiment Lab Course Online
+ * Copyright © 2021 Ruhr-Universität Bochum, Institut für Experimentalphysik I
+ * https://www.ep1.ruhr-uni-bochum.de/
+ *
+ * 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 <QDir>
+
+#include "cacertificates.h"
+
+static void initResourcesOutsideNamespace()
+{
+#ifdef LIBFP311ONLINE_LOAD_DEVEL_CERT_RESOURCE
+    Q_INIT_RESOURCE(devel_cert);
+#endif
+}
+
+using namespace Fp311Online;
+
+QList<QSslCertificate> CaCertificates::getCaCertificatesFromResource()
+{
+    static bool initializedResource = false;
+    if (!initializedResource) {
+        initResourcesOutsideNamespace();
+        initializedResource = true;
+    }
+
+    return QSslCertificate::fromPath(
+               QStringLiteral(":/crypto/*.pem"),
+               QSsl::Pem,
+               QRegExp::Wildcard
+           );
+}
diff --git a/src/cacertificates.h b/src/cacertificates.h
new file mode 100644
index 0000000..6d9c62b
--- /dev/null
+++ b/src/cacertificates.h
@@ -0,0 +1,51 @@
+/**
+ * @file cacertificates.h
+ *
+ * @author Tobias Triffterer
+ *
+ * @brief Retrieve Certificates from Qt Resources
+ *
+ * Rutherford Experiment Lab Course Online
+ * Copyright © 2021 Ruhr-Universität Bochum, Institut für Experimentalphysik I
+ * https://www.ep1.ruhr-uni-bochum.de/
+ *
+ * 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 LIBFP311ONLINE_CACERTIFICATES_H
+#define LIBFP311ONLINE_CACERTIFICATES_H
+
+#include <QList>
+#include <QSslCertificate>
+
+namespace Fp311Online
+{
+
+class CaCertificates
+{
+public:
+    static QList<QSslCertificate> getCaCertificatesFromResource();
+
+    CaCertificates() = delete;
+    ~CaCertificates() = delete;
+    CaCertificates(const CaCertificates& other) = delete;
+    CaCertificates(CaCertificates&& other) = delete;
+    CaCertificates& operator=(const CaCertificates& other) = delete;
+    CaCertificates& operator=(CaCertificates&& other) = delete;
+};
+
+}
+
+#endif // LIBFP311ONLINE_CACERTIFICATES_H
-- 
GitLab