Messagelib

dkimkeyrecord.cpp
1/*
2 SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "dkimkeyrecord.h"
8#include "messageviewer_dkimcheckerdebug.h"
9
10using namespace MessageViewer;
11
12DKIMKeyRecord::DKIMKeyRecord() = default;
13
14bool DKIMKeyRecord::parseKey(const QString &key)
15{
16 // qDebug() << " key " << key;
17 QString newKey = key;
18 if (newKey.isEmpty()) {
19 qCWarning(MESSAGEVIEWER_DKIMCHECKER_LOG) << "Error: trying to parse empty key";
20 return false;
21 }
23 const QStringList items = newKey.split(QLatin1Char(';'));
24 for (int i = 0; i < items.count(); ++i) {
25 const QString elem = items.at(i).trimmed();
26 if (elem.startsWith(QLatin1StringView("v="))) {
27 mVersion = elem.right(elem.length() - 2);
28 } else if (elem.startsWith(QLatin1StringView("h="))) {
29 // Parse multi array.
30 mHashAlgorithm = elem.right(elem.length() - 2).split(QLatin1Char(':'));
31 } else if (elem.startsWith(QLatin1StringView("k="))) { // Key type (rsa by default)
32 mKeyType = elem.right(elem.length() - 2);
33 } else if (elem.startsWith(QLatin1StringView("n="))) { // Notes (optional empty by default)
34 mNote = elem.right(elem.length() - 2);
35 } else if (elem.startsWith(QLatin1StringView("p="))) { // Public key
36 mPublicKey = elem.right(elem.length() - 2).remove(QLatin1Char(' '));
37 } else if (elem.startsWith(QLatin1StringView("s="))) { // Service Default is "*"
38 // Service Type (plain-text; OPTIONAL; default is "*"). A colon-
39 // separated list of service types to which this record applies.
40 // Verifiers for a given service type MUST ignore this record if the
41 // appropriate type is not listed. Unrecognized service types MUST
42 // be ignored. Currently defined service types are as follows:
43 const QStringList lst = elem.right(elem.length() - 2).split(QLatin1Char(':'));
44 for (const QString &service : lst) {
45 if (service == QLatin1Char('*') || service == QLatin1StringView("email")) {
46 mService = service;
47 }
48 }
49 } else if (elem.startsWith(QLatin1StringView("t="))) { // Flag
50 // t= Flags, represented as a colon-separated list of names (plain-
51 // text; OPTIONAL, default is no flags set). Unrecognized flags MUST
52 // be ignored. The defined flags are as follows:
53
54 // y This domain is testing DKIM. Verifiers MUST NOT treat messages
55 // from Signers in testing mode differently from unsigned email,
56 // even should the signature fail to verify. Verifiers MAY wish
57 // to track testing mode results to assist the Signer.
58
59 // s Any DKIM-Signature header fields using the "i=" tag MUST have
60 // the same domain value on the right-hand side of the "@" in the
61 // "i=" tag and the value of the "d=" tag. That is, the "i="
62 // domain MUST NOT be a subdomain of "d=". Use of this flag is
63 // RECOMMENDED unless subdomaining is required.
64 mFlags = elem.right(elem.length() - 2).split(QLatin1Char(':'));
65 }
66 }
67 if (mVersion.isEmpty()) { // It's optional
68 mVersion = QStringLiteral("DKIM1");
69 }
70 if (mKeyType.isEmpty()) { // Rsa by default
71 mKeyType = QStringLiteral("rsa");
72 }
73 if (mService.isEmpty()) {
74 mService = QLatin1Char('*');
75 }
76 return true;
77}
78
79QString DKIMKeyRecord::version() const
80{
81 return mVersion;
82}
83
84void DKIMKeyRecord::setVersion(const QString &version)
85{
86 mVersion = version;
87}
88
89QString DKIMKeyRecord::keyType() const
90{
91 return mKeyType;
92}
93
94void DKIMKeyRecord::setKeyType(const QString &keyType)
95{
96 mKeyType = keyType;
97}
98
99QString DKIMKeyRecord::note() const
100{
101 return mNote;
102}
103
104void DKIMKeyRecord::setNote(const QString &note)
105{
106 mNote = note;
107}
108
109QString DKIMKeyRecord::publicKey() const
110{
111 return mPublicKey;
112}
113
114void DKIMKeyRecord::setPublicKey(const QString &publicKey)
115{
116 mPublicKey = publicKey;
117}
118
119QString DKIMKeyRecord::service() const
120{
121 return mService;
122}
123
124void DKIMKeyRecord::setService(const QString &service)
125{
126 mService = service;
127}
128
129QStringList DKIMKeyRecord::flags() const
130{
131 return mFlags;
132}
133
134void DKIMKeyRecord::setFlags(const QStringList &flags)
135{
136 mFlags = flags;
137}
138
139bool DKIMKeyRecord::operator==(const DKIMKeyRecord &other) const
140{
141 return mVersion == other.version() && mNote == other.note() && mPublicKey == other.publicKey() && mService == other.service()
142 && mHashAlgorithm == other.hashAlgorithm() && mFlags == other.flags();
143}
144
145QStringList DKIMKeyRecord::hashAlgorithm() const
146{
147 return mHashAlgorithm;
148}
149
150void DKIMKeyRecord::setHashAlgorithm(const QStringList &hashAlgorithm)
151{
152 mHashAlgorithm = hashAlgorithm;
153}
154
156{
157 d << "mVersion " << t.version();
158 d << "mKeyType " << t.keyType();
159 d << "mNote " << t.note();
160 d << "mPublicKey " << t.publicKey();
161 d << "mService " << t.service();
162 d << "mHashAlgorithm " << t.hashAlgorithm();
163 d << "mFlags " << t.flags();
164 return d;
165}
The DKIMKeyRecord class.
KDB_EXPORT KDbVersionInfo version()
QDebug operator<<(QDebug dbg, const PerceptualColor::LchaDouble &value)
const_reference at(qsizetype i) const const
qsizetype count() const const
bool isEmpty() const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QString right(qsizetype n) const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:12:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.