summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAhmad Samir <[email protected]>2025-04-30 17:30:42 +0300
committerAhmad Samir <[email protected]>2025-05-14 04:41:13 +0300
commit2be51c692307f067bb5bdc011e5f3dca1dbe3ff8 (patch)
tree81367961331585b4ad02dd8773b467af929d67e5 /src
parent2875c4358beedd0997b60d21df4b95dbfec4f9a6 (diff)
QSslCertificate: add fromFile() method
QSslCertificate::fromPath() does some extra work: - matching wildcard glob or regular expression patterns - checks if the string it's called on is a file or a dir That extra work isn't needed when you already have the path to a specific certificate file. E.g. qtlsbackend_openssl.cpp:systemCaCertificates() used to call fromPath() on *.pem/*.crt files that it got from iterating over system certifcates dirs. This also de-duplicates the code in fromPath(). [ChangeLog][QtNetwork][QSslCertificate] Added fromFile() method. Change-Id: I92ab358e4711866dd4510da42c47905c7dae58b1 Reviewed-by: Ivan Solovev <[email protected]> Reviewed-by: MÃ¥rten Nordheim <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/network/ssl/qsslcertificate.cpp45
-rw-r--r--src/network/ssl/qsslcertificate.h2
-rw-r--r--src/plugins/tls/openssl/qtlsbackend_openssl.cpp2
3 files changed, 33 insertions, 16 deletions
diff --git a/src/network/ssl/qsslcertificate.cpp b/src/network/ssl/qsslcertificate.cpp
index af3e47f66f9..38fea369055 100644
--- a/src/network/ssl/qsslcertificate.cpp
+++ b/src/network/ssl/qsslcertificate.cpp
@@ -633,6 +633,9 @@ QList<QSslCertificate> QSslCertificate::fromPath(const QString &path,
if (path.isEmpty())
return {};
+ if (syntax == PatternSyntax::FixedString && QFileInfo(path).isFile())
+ return fromFile(path, format);
+
// $, (,), *, +, ., ?, [, ,], ^, {, | and }.
// make sure to use the same path separators on Windows and Unix like systems.
@@ -665,15 +668,8 @@ QList<QSslCertificate> QSslCertificate::fromPath(const QString &path,
pathPrefix = {};
} else {
// Check if the path is a file.
- if (QFileInfo(sourcePath).isFile()) {
- QFile file(sourcePath);
- QIODevice::OpenMode openMode = QIODevice::ReadOnly;
- if (format == QSsl::Pem)
- openMode |= QIODevice::Text;
- if (file.open(openMode))
- return QSslCertificate::fromData(file.readAll(), format);
- return QList<QSslCertificate>();
- }
+ if (QFileInfo(sourcePath).isFile())
+ return fromFile(sourcePath, format);
}
// Special case - if the prefix ends up being nothing, use "." instead.
@@ -710,12 +706,7 @@ QList<QSslCertificate> QSslCertificate::fromPath(const QString &path,
continue;
#endif
- QFile file(filePath);
- QIODevice::OpenMode openMode = QIODevice::ReadOnly;
- if (format == QSsl::Pem)
- openMode |= QIODevice::Text;
- if (file.open(openMode))
- certs += QSslCertificate::fromData(file.readAll(), format);
+ certs += QSslCertificate::fromFile(filePath, format);
}
return certs;
}
@@ -760,6 +751,30 @@ QList<QSslCertificate> QSslCertificate::fromData(const QByteArray &data, QSsl::E
return reader(data, -1);
}
+/*!
+ \since 6.10
+
+ Reads the data from the file \a filePath and parses all certificates
+ that are encoded in the specified \a format and returns a list of
+ QSslCertificate objects.
+
+ If \a filePath isn't a regular file, this method will return an empty
+ list.
+
+ \sa fromData(), fromPath()
+*/
+QList<QSslCertificate> QSslCertificate::fromFile(const QString &filePath,
+ QSsl::EncodingFormat format)
+{
+ QFile file(filePath);
+ QIODevice::OpenMode openMode = QIODevice::ReadOnly;
+ if (format == QSsl::Pem)
+ openMode |= QIODevice::Text;
+ if (file.open(openMode))
+ return QSslCertificate::fromData(file.readAll(), format);
+ return {};
+}
+
#ifndef QT_NO_SSL
/*!
Verifies a certificate chain. The chain to be verified is passed in the
diff --git a/src/network/ssl/qsslcertificate.h b/src/network/ssl/qsslcertificate.h
index e34fa4d92a3..17f931c1268 100644
--- a/src/network/ssl/qsslcertificate.h
+++ b/src/network/ssl/qsslcertificate.h
@@ -107,6 +107,8 @@ public:
QIODevice *device, QSsl::EncodingFormat format = QSsl::Pem);
static QList<QSslCertificate> fromData(
const QByteArray &data, QSsl::EncodingFormat format = QSsl::Pem);
+ static QList<QSslCertificate> fromFile(
+ const QString &filePath, QSsl::EncodingFormat format = QSsl::Pem);
#ifndef QT_NO_SSL
static QList<QSslError> verify(const QList<QSslCertificate> &certificateChain, const QString &hostName = QString());
diff --git a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
index 6a55f275943..bce31734f94 100644
--- a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
+++ b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
@@ -410,7 +410,7 @@ QList<QSslCertificate> systemCaCertificates()
}
}
for (const QString& file : std::as_const(certFiles))
- systemCerts.append(QSslCertificate::fromPath(file, QSsl::Pem));
+ systemCerts.append(QSslCertificate::fromFile(file, QSsl::Pem));
}
#endif // platform
#ifdef QSSLSOCKET_DEBUG