From 441063f8f8116a3524c63eb59fa98bcdd58d7e83 Mon Sep 17 00:00:00 2001 From: Mikkel Krautz Date: Mon, 20 Feb 2017 23:44:49 +0100 Subject: [PATCH] Server, Cert: add Server::privateKeyFromPEM() method. This adds a new method, Server::privateKeyFromPEM(), a helper method for loading private keys. In various places throughout the codebase, we use the same sequence of operations for loading private keys from a PEM bytestream. This method moves that sequence to a single method, to avoid the unnecessary duplication that is currently going on. --- src/murmur/Cert.cpp | 12 ++++++++++++ src/murmur/Server.h | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/murmur/Cert.cpp b/src/murmur/Cert.cpp index b16351b57..5c5a37c8c 100644 --- a/src/murmur/Cert.cpp +++ b/src/murmur/Cert.cpp @@ -70,6 +70,18 @@ bool Server::isKeyForCert(const QSslKey &key, const QSslCertificate &cert) { return false; } +QSslKey Server::privateKeyFromPEM(const QByteArray &buf, const QByteArray &pass) { + QSslKey key; + key = QSslKey(buf, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, pass); + if (key.isNull()) + key = QSslKey(buf, QSsl::Dsa, QSsl::Pem, QSsl::PrivateKey, pass); +#if QT_VERSION >= 0x050000 + if (key.isNull()) + key = QSslKey(buf, QSsl::Ec, QSsl::Pem, QSsl::PrivateKey, pass); +#endif + return key; +} + void Server::initializeCert() { QByteArray crt, key, pass, dhparams; diff --git a/src/murmur/Server.h b/src/murmur/Server.h index b8cddf433..0d4d9ef73 100644 --- a/src/murmur/Server.h +++ b/src/murmur/Server.h @@ -169,6 +169,11 @@ class Server : public QThread { // Certificate stuff, implemented partially in Cert.cpp public: static bool isKeyForCert(const QSslKey &key, const QSslCertificate &cert); + /// Attempt to load a private key in PEM format from |buf|. + /// If |passphrase| is non-empty, it will be used for decrypting the private key in |buf|. + /// If a valid RSA, DSA or EC key is found, it is returned. + /// If no valid private key is found, a null QSslKey is returned. + static QSslKey privateKeyFromPEM(const QByteArray &buf, const QByteArray &pass = QByteArray()); void initializeCert(); const QString getDigest() const;