10#include "ksslcertificatemanager.h"
11#include "ksslcertificatemanager_p.h"
12#include "kssld_adaptor.h"
15#include <KConfigGroup>
17#include <KPluginFactory>
26 : config(QStringLiteral(
"ksslcertificatemanager"),
KConfig::SimpleConfig)
30 QSslError::SslError err;
35 const static strErr strError[] = {{
"NoError", QSslError::NoError},
36 {
"UnknownError", QSslError::UnspecifiedError},
37 {
"InvalidCertificateAuthority", QSslError::InvalidCaCertificate},
38 {
"InvalidCertificate", QSslError::UnableToDecodeIssuerPublicKey},
39 {
"CertificateSignatureFailed", QSslError::CertificateSignatureFailed},
40 {
"SelfSignedCertificate", QSslError::SelfSignedCertificate},
41 {
"RevokedCertificate", QSslError::CertificateRevoked},
42 {
"InvalidCertificatePurpose", QSslError::InvalidPurpose},
43 {
"RejectedCertificate", QSslError::CertificateRejected},
44 {
"UntrustedCertificate", QSslError::CertificateUntrusted},
45 {
"ExpiredCertificate", QSslError::CertificateExpired},
46 {
"HostNameMismatch", QSslError::HostNameMismatch},
47 {
"UnableToGetLocalIssuerCertificate", QSslError::UnableToGetLocalIssuerCertificate},
48 {
"InvalidNotBeforeField", QSslError::InvalidNotBeforeField},
49 {
"InvalidNotAfterField", QSslError::InvalidNotAfterField},
50 {
"CertificateNotYetValid", QSslError::CertificateNotYetValid},
51 {
"SubjectIssuerMismatch", QSslError::SubjectIssuerMismatch},
52 {
"AuthorityIssuerSerialNumberMismatch", QSslError::AuthorityIssuerSerialNumberMismatch},
53 {
"SelfSignedCertificateInChain", QSslError::SelfSignedCertificateInChain},
54 {
"UnableToVerifyFirstCertificate", QSslError::UnableToVerifyFirstCertificate},
55 {
"UnableToDecryptCertificateSignature", QSslError::UnableToDecryptCertificateSignature},
56 {
"UnableToGetIssuerCertificate", QSslError::UnableToGetIssuerCertificate}};
58 for (
const strErr &row : strError) {
60 stringToSslError.insert(s, row.err);
61 sslErrorToString.insert(row.err, s);
70KSSLD::KSSLD(
QObject *parent,
const QVariantList &)
72 , d(new KSSLDPrivate())
74 new KSSLDAdaptor(
this);
78KSSLD::~KSSLD() =
default;
82 if (rule.hostName().
isEmpty()) {
89 QString dtString = QStringLiteral(
"ExpireUTC ");
93 if (rule.isRejected()) {
94 sl.
append(QStringLiteral(
"Reject"));
96 const auto ignoredErrors = rule.ignoredErrors();
97 for (QSslError::SslError e : ignoredErrors) {
98 sl.
append(d->sslErrorToString.value(e));
102 if (!group.
hasKey(
"CertificatePEM")) {
106 else if (group.
readEntry(
"CertificatePEM") != rule.certificate().
toPem()) {
116 clearRule(rule.certificate(), rule.hostName());
129void KSSLD::pruneExpiredRules()
133 const QStringList groupNames = d->config.groupList();
134 for (
const QString &groupName : groupNames) {
136 const QStringList keys = d->config.group(groupName).keyList();
137 for (
const QString &key : keys) {
147static QString normalizeSubdomains(
const QString &hostName,
int *namePartsCount)
151 bool wasPrevDot =
true;
152 const int length = hostName.
length();
153 for (
int i = 0; i < length; i++) {
154 const QChar c = hostName.
at(i);
156 if (wasPrevDot || (i + 1 == hostName.
length())) {
172 *namePartsCount = partsCount;
182 bool foundHostName =
false;
184 int needlePartsCount;
185 QString needle = normalizeSubdomains(hostName, &needlePartsCount);
188 if (group.
hasKey(needle)) {
190 if (needlePartsCount >= 1) {
191 foundHostName =
true;
197 while (--needlePartsCount >= 2) {
199 Q_ASSERT(dotIndex > 0);
200 needle.
remove(0, dotIndex - 1);
202 if (group.
hasKey(needle)) {
203 foundHostName =
true;
210 if (!foundHostName) {
221 if (sl.
size() >= 2) {
240 bool isRejected =
false;
241 for (
const QString &s : std::as_const(sl)) {
244 ignoredErrors.
clear();
247 if (!d->stringToSslError.contains(s)) {
250 ignoredErrors.
append(d->stringToSslError.value(s));
254 ret.setExpiryDateTime(expiryDt);
255 ret.setRejected(isRejected);
256 ret.setIgnoredErrors(ignoredErrors);
261#include "moc_kssld.cpp"
262#include "moc_kssld_adaptor.cpp"
void deleteEntry(const char *key, WriteConfigFlags pFlags=Normal)
bool hasKey(const char *key) const
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
void deleteGroup(const QString &group, WriteConfigFlags flags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
QStringList keyList() const
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
QByteArray toHex(char separator) const const
QDateTime currentDateTime()
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
bool isValid() const const
QString toString(QStringView format, QCalendar cal) const const
void append(QList< T > &&value)
qsizetype size() const const
QByteArray digest(QCryptographicHash::Algorithm algorithm) const const
QByteArray toPem() const const
QString & append(QChar ch)
const QChar at(qsizetype position) const const
QString fromLatin1(QByteArrayView str)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const