From 4f658d04da03953358959c0a0921e564cf67ecb2 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Wed, 9 Sep 2020 15:11:29 +0200 Subject: Fix a rather sloppy test case in tst_QSslKey It's not guaranteed that all curves we want to use are supported by a specific build of OpenSSL library we have to link with. Filter out files that contain EC, which is not among the curves, reported by QSslConfiguration::supportedEllipticCurves. Fixes: QTBUG-46203 Pick-to: 5.15 Change-Id: I70d7e6fcacb3d81f8c771e4a8d6cca06295e7474 Reviewed-by: Timur Pocheptsov --- tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp | 57 +++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 6 deletions(-) (limited to 'tests/auto/network') diff --git a/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp b/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp index ce223a53ef..b1dbd2773b 100644 --- a/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp +++ b/tests/auto/network/ssl/qsslkey/tst_qsslkey.cpp @@ -31,10 +31,16 @@ #include #include #include +#include +#include #include #include +#include +#include +#include + #ifdef QT_BUILD_INTERNAL #ifndef QT_NO_SSL #include "private/qsslkey_p.h" @@ -45,6 +51,8 @@ #endif #endif +#include + class tst_QSslKey : public QObject { Q_OBJECT @@ -65,6 +73,8 @@ class tst_QSslKey : public QObject QList keyInfoList; void createPlainTestRows(bool pemOnly = false); +public: + tst_QSslKey(); public slots: void initTestCase(); @@ -100,8 +110,46 @@ private slots: #endif private: QString testDataDir; + + bool fileContainsUnsupportedEllipticCurve(const QString &fileName) const; + QVector unsupportedCurves; }; +tst_QSslKey::tst_QSslKey() +{ + const QString expectedCurves[] = { + // See how we generate them in keys/genkey.sh. + QStringLiteral("secp224r1"), + QStringLiteral("prime256v1"), + QStringLiteral("secp384r1"), + QStringLiteral("brainpoolP256r1"), + QStringLiteral("brainpoolP384r1"), + QStringLiteral("brainpoolP512r1") + }; + const auto supportedCurves = QSslConfiguration::supportedEllipticCurves(); + + for (const auto &requestedEc : expectedCurves) { + auto pos = std::find_if(supportedCurves.begin(), supportedCurves.end(), + [&requestedEc](const auto &supported) { + return requestedEc == supported.shortName(); + }); + if (pos == supportedCurves.end()) { + qWarning() << "EC with the name:" << requestedEc + << "is not supported by your build of OpenSSL and will not be tested."; + unsupportedCurves.push_back(requestedEc); + } + } +} + +bool tst_QSslKey::fileContainsUnsupportedEllipticCurve(const QString &fileName) const +{ + for (const auto &name : unsupportedCurves) { + if (fileName.contains(name)) + return true; + } + return false; +} + void tst_QSslKey::initTestCase() { testDataDir = QFileInfo(QFINDTESTDATA("rsa-without-passphrase.pem")).absolutePath(); @@ -114,6 +162,8 @@ void tst_QSslKey::initTestCase() const QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::Readable); QRegularExpression rx(QLatin1String("^(rsa|dsa|dh|ec)-(pub|pri)-(\\d+)-?[\\w-]*\\.(pem|der)$")); for (const QFileInfo &fileInfo : fileInfoList) { + if (fileContainsUnsupportedEllipticCurve(fileInfo.fileName())) + continue; auto match = rx.match(fileInfo.fileName()); if (match.hasMatch()) { keyInfoList << KeyInfo( @@ -239,14 +289,9 @@ void tst_QSslKey::constructorHandle() BIO* bio = q_BIO_new(q_BIO_s_mem()); q_BIO_write(bio, pem.constData(), pem.length()); EVP_PKEY *origin = func(bio, nullptr, nullptr, static_cast(passphrase.data())); -#if QT_CONFIG(opensslv11) + Q_ASSERT(origin); q_EVP_PKEY_up_ref(origin); -#endif QSslKey key(origin, type); -#if !QT_CONFIG(opensslv11) - q_BIO_write(bio, pem.constData(), pem.length()); - origin = func(bio, nullptr, nullptr, static_cast(passphrase.data())); -#endif q_BIO_free(bio); EVP_PKEY *handle = q_EVP_PKEY_new(); -- cgit v1.2.3