18 #define CERTSTORAGE ".config/openslx/pvs2/"
21 #include "../../shared/util.h"
29 #include <QMessageBox>
31 #include <QCoreApplication>
35 static QHash<QString, QSslCertificate>
_certs;
36 static QHash<QString, QSslKey>
_keys;
39 static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert);
43 if (_keys.contains(name)) {
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);
53 QString certFile = certDir.append(name);
54 QString keyFile = certFile;
55 keyFile.append(
".rsa");
56 certFile.append(
".crt");
58 if (!
loadFiles(keyFile, certFile, key, cert)) {
60 if (!
loadFiles(keyFile, certFile, key, cert)) {
61 qDebug() <<
"error while creating cert and key files";
65 _certs.insert(name, cert);
66 _keys.insert(name, key);
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);
79 static bool loadFiles(QString& keyFile, QString& certFile, QSslKey &key, QSslCertificate &cert)
81 QFileInfo keyInfo(keyFile);
82 QFileInfo certInfo(certFile);
83 if (keyInfo.exists() && certInfo.exists()) {
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();
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
111 p.start(QStringLiteral(
"chmod"), {
"0600", key, cert });
112 p.waitForFinished(500);
static bool loadFiles(QString &keyFile, QString &certFile, QSslKey &key, QSslCertificate &cert)
bool getPrivateKeyAndCert(const QString &name, QSslKey &key, QSslCertificate &cert)
static QHash< QString, QSslCertificate > _certs
static void generateFiles(QString &key, QString &cert)
static QHash< QString, QSslKey > _keys