summaryrefslogtreecommitdiffstats
path: root/src/server/net/certmanager.cpp
diff options
context:
space:
mode:
authorChristian Klinger2016-09-29 15:40:18 +0200
committerChristian Klinger2016-09-29 15:40:18 +0200
commit3d82bfc698165bb20f3c38d42f2b77ab855c1edf (patch)
tree2c170bcdd5c89259bb0b530fa11fdfe701c99850 /src/server/net/certmanager.cpp
parentadded an information dialog that displays ip and hostname. (diff)
downloadpvs2-3d82bfc698165bb20f3c38d42f2b77ab855c1edf.tar.gz
pvs2-3d82bfc698165bb20f3c38d42f2b77ab855c1edf.tar.xz
pvs2-3d82bfc698165bb20f3c38d42f2b77ab855c1edf.zip
astyle.
Diffstat (limited to 'src/server/net/certmanager.cpp')
-rw-r--r--src/server/net/certmanager.cpp184
1 files changed, 90 insertions, 94 deletions
diff --git a/src/server/net/certmanager.cpp b/src/server/net/certmanager.cpp
index b85966a..e91fed3 100644
--- a/src/server/net/certmanager.cpp
+++ b/src/server/net/certmanager.cpp
@@ -1,94 +1,90 @@
-/*
- # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
- #
- # This program is free software distributed under the GPL version 2.
- # See http://openslx.org/COPYING
- #
- # If you have any feedback please consult http://openslx.org/feedback and
- # send your suggestions, praise, or complaints to feedback@openslx.org
- #
- # General information about OpenSLX can be found at http://openslx.org/
- # -----------------------------------------------------------------------------
- # src/util/CertManager.cpp
- # - Manage SSL certificates
- # - provide access by name
- # -----------------------------------------------------------------------------
- */
-
-#include "certmanager.h"
-#include "../util/util.h"
-#include <QMap>
-#include <QDir>
-#include <QDebug>
-#include <QFileInfo>
-#include <QSettings>
-#include <cstdlib>
-
-namespace CertManager
-{
-static QMap<QString, QSslCertificate> _certs;
-static QMap<QString, QSslKey> _keys;
-
-static void generateFiles(QString& key, QString& cert);
-static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert);
-
-bool getPrivateKeyAndCert(const QString &name, QSslKey &key, QSslCertificate &cert)
-{
- if (_keys.contains(name))
- {
- key = _keys[name];
- cert = _certs[name];
- return true;
- }
- QString certFile = QDir::homePath().append("/").append(CERTSTORAGE).append(name);
- QString keyFile = certFile;
- keyFile.append(".rsa");
- certFile.append(".crt");
- //
- if (!loadFiles(keyFile, certFile, key, cert))
- {
- generateFiles(keyFile, certFile);
- if (!loadFiles(keyFile, certFile, key, cert))
- qDebug() << "error while creating cert and key files\n";
- return false;
- }
- _certs.insert(name, cert);
- _keys.insert(name, key);
- return true;
-}
-
-static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert)
-{
- QFileInfo keyInfo(keyFile);
- QFileInfo certInfo(certFile);
- if (keyInfo.exists() && certInfo.exists())
- { // Both files exist, see if they're valid and return
- QFile kf(keyFile);
- kf.open(QFile::ReadOnly);
- key = QSslKey(&kf, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
- QList<QSslCertificate> certlist = QSslCertificate::fromPath(certFile);
- if (!key.isNull() && !certlist.empty())
- {
- cert = certlist.first();
- if (!cert.isNull())
- {
- return true;
- }
- }
- }
- return false;
-}
-
-static void generateFiles(QString& key, QString& cert)
-{
- char tmp[1000];
- remove(key.toLocal8Bit().data());
- remove(cert.toLocal8Bit().data());
- snprintf(tmp, 1000,
- "openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -subj '/C=DE/ST=BaWue/L=Freiburg/CN=openslx.org' -keyout \"%s\" -out \"%s\"",
- key.toLocal8Bit().data(), cert.toLocal8Bit().data());
- system(tmp);
- snprintf(tmp, 1000, "chmod 0600 \"%s\" \"%s\"", key.toLocal8Bit().data(), cert.toLocal8Bit().data());
- system(tmp);
-}
-}
+/*
+ # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
+ #
+ # This program is free software distributed under the GPL version 2.
+ # See http://openslx.org/COPYING
+ #
+ # If you have any feedback please consult http://openslx.org/feedback and
+ # send your suggestions, praise, or complaints to feedback@openslx.org
+ #
+ # General information about OpenSLX can be found at http://openslx.org/
+ # -----------------------------------------------------------------------------
+ # src/util/CertManager.cpp
+ # - Manage SSL certificates
+ # - provide access by name
+ # -----------------------------------------------------------------------------
+ */
+
+#include "certmanager.h"
+#include "../util/util.h"
+#include <QMap>
+#include <QDir>
+#include <QDebug>
+#include <QFileInfo>
+#include <QSettings>
+#include <cstdlib>
+
+namespace CertManager
+{
+static QMap<QString, QSslCertificate> _certs;
+static QMap<QString, QSslKey> _keys;
+
+static void generateFiles(QString& key, QString& cert);
+static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert);
+
+bool getPrivateKeyAndCert(const QString &name, QSslKey &key, QSslCertificate &cert)
+{
+ if (_keys.contains(name)) {
+ key = _keys[name];
+ cert = _certs[name];
+ return true;
+ }
+ QString certFile = QDir::homePath().append("/").append(CERTSTORAGE).append(name);
+ QString keyFile = certFile;
+ keyFile.append(".rsa");
+ certFile.append(".crt");
+ //
+ if (!loadFiles(keyFile, certFile, key, cert)) {
+ generateFiles(keyFile, certFile);
+ if (!loadFiles(keyFile, certFile, key, cert))
+ qDebug() << "error while creating cert and key files\n";
+ return false;
+ }
+ _certs.insert(name, cert);
+ _keys.insert(name, key);
+ return true;
+}
+
+static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert)
+{
+ QFileInfo keyInfo(keyFile);
+ QFileInfo certInfo(certFile);
+ if (keyInfo.exists() && certInfo.exists()) {
+ // Both files exist, see if they're valid and return
+ QFile kf(keyFile);
+ kf.open(QFile::ReadOnly);
+ key = QSslKey(&kf, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
+ QList<QSslCertificate> certlist = QSslCertificate::fromPath(certFile);
+ if (!key.isNull() && !certlist.empty()) {
+ cert = certlist.first();
+ if (!cert.isNull()) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+static void generateFiles(QString& key, QString& cert)
+{
+ char tmp[1000];
+ remove(key.toLocal8Bit().data());
+ remove(cert.toLocal8Bit().data());
+ snprintf(tmp, 1000,
+ "openssl req -x509 -nodes -days 3650 -newkey rsa:1024 -subj '/C=DE/ST=BaWue/L=Freiburg/CN=openslx.org' -keyout \"%s\" -out \"%s\"",
+ key.toLocal8Bit().data(), cert.toLocal8Bit().data());
+ system(tmp);
+ snprintf(tmp, 1000, "chmod 0600 \"%s\" \"%s\"", key.toLocal8Bit().data(), cert.toLocal8Bit().data());
+ system(tmp);
+}
+}