Pool Video Switch v2
Software video switch for distributed remote display in a lecture environment
certmanager.cpp
Go to the documentation of this file.
1 /*
2  # Copyright (c) 2009 - OpenSLX Project, Computer Center University of Freiburg
3  #
4  # This program is free software distributed under the GPL version 2.
5  # See http://openslx.org/COPYING
6  #
7  # If you have any feedback please consult http://openslx.org/feedback and
8  # send your suggestions, praise, or complaints to [email protected]
9  #
10  # General information about OpenSLX can be found at http://openslx.org/
11  # -----------------------------------------------------------------------------
12  # src/util/CertManager.cpp
13  # - Manage SSL certificates
14  # - provide access by name
15  # -----------------------------------------------------------------------------
16  */
17 
18 #define CERTSTORAGE ".config/openslx/pvs2/"
19 
20 #include "certmanager.h"
21 #include "../../shared/util.h"
22 // Remove in future - see comment in util.h
23 #undef errorOccurred
24 
25 #include <QHash>
26 #include <QDir>
27 #include <QDebug>
28 #include <QFileInfo>
29 #include <QMessageBox>
30 #include <QProcess>
31 #include <QCoreApplication>
32 
33 namespace CertManager
34 {
35 static QHash<QString, QSslCertificate> _certs;
36 static QHash<QString, QSslKey> _keys;
37 
38 static void generateFiles(QString& key, QString& cert);
39 static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert);
40 
41 bool getPrivateKeyAndCert(const QString &name, QSslKey &key, QSslCertificate &cert)
42 {
43  if (_keys.contains(name)) {
44  key = _keys[name];
45  cert = _certs[name];
46  return true;
47  }
48  QString certDir = QDir::homePath().append("/").append(CERTSTORAGE);
49  if (!QDir::root().mkpath(certDir)) {
50  certDir = QString("/tmp/") + QString::number(slxrand()) + "-" + QString::number(slxrand()) + "/";
51  QDir::root().mkpath(certDir);
52  }
53  QString certFile = certDir.append(name);
54  QString keyFile = certFile;
55  keyFile.append(".rsa");
56  certFile.append(".crt");
57  //
58  if (!loadFiles(keyFile, certFile, key, cert)) {
59  generateFiles(keyFile, certFile);
60  if (!loadFiles(keyFile, certFile, key, cert)) {
61  qDebug() << "error while creating cert and key files";
62  return false;
63  }
64  }
65  _certs.insert(name, cert);
66  _keys.insert(name, key);
67  return true;
68 }
69 
70 void fatal()
71 {
72  QMessageBox::critical(nullptr, QObject::tr("OpenSSL error", "CertManager"),
73  QObject::tr("Could not generate certificates for secure connections.\n"
74  "PVS will not work.\n\n"
75  "Press OK to quit.", "CertManager"));
76  QCoreApplication::exit(1);
77 }
78 
79 static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert)
80 {
81  QFileInfo keyInfo(keyFile);
82  QFileInfo certInfo(certFile);
83  if (keyInfo.exists() && certInfo.exists()) {
84  // Both files exist, see if they're valid and return
85  QFile kf(keyFile);
86  kf.open(QFile::ReadOnly);
87  key = QSslKey(&kf, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey);
88  QList<QSslCertificate> certlist = QSslCertificate::fromPath(certFile);
89  if (!key.isNull() && !certlist.empty()) {
90  cert = certlist.first();
91  if (!cert.isNull()) {
92  return true;
93  }
94  }
95  }
96  return false;
97 }
98 
99 static void generateFiles(QString& key, QString& cert)
100 {
101  QProcess p;
102  QFile::remove(key);
103  QFile::remove(cert);
104  p.setProcessChannelMode(QProcess::ForwardedChannels);
105  p.start(QStringLiteral("openssl"), {
106  "req", "-x509", "-nodes", "-days", "5000", "-newkey", "rsa:4096",
107  "-subj", "/C=DE/ST=BaWue/L=Freiburg/CN=openslx.org",
108  "-keyout", key, "-out", cert
109  });
110  p.waitForFinished();
111  p.start(QStringLiteral("chmod"), { "0600", key, cert });
112  p.waitForFinished(500);
113 }
114 
115 }
void fatal()
Definition: certmanager.cpp:70
#define CERTSTORAGE
Definition: certmanager.cpp:18
static bool loadFiles(QString &keyFile, QString &certFile, QSslKey &key, QSslCertificate &cert)
Definition: certmanager.cpp:79
#define slxrand()
Definition: util.h:19
bool getPrivateKeyAndCert(const QString &name, QSslKey &key, QSslCertificate &cert)
Definition: certmanager.cpp:41
static QHash< QString, QSslCertificate > _certs
Definition: certmanager.cpp:35
static void generateFiles(QString &key, QString &cert)
Definition: certmanager.cpp:99
static QHash< QString, QSslKey > _keys
Definition: certmanager.cpp:36