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.
This commit is contained in:
Mikkel Krautz 2017-02-20 23:44:49 +01:00
parent e75e737130
commit 441063f8f8
2 changed files with 17 additions and 0 deletions

View File

@ -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;

View File

@ -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;