Messagelib

dkimkeyrecord.cpp
1 /*
2  SPDX-FileCopyrightText: 2019-2023 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "dkimkeyrecord.h"
8 #include "messageviewer_dkimcheckerdebug.h"
9 
10 using namespace MessageViewer;
11 
12 DKIMKeyRecord::DKIMKeyRecord() = default;
13 
14 bool 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  }
22  newKey.replace(QLatin1String("; "), QLatin1String(";"));
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(QLatin1String("v="))) {
27  mVersion = elem.right(elem.length() - 2);
28  } else if (elem.startsWith(QLatin1String("h="))) {
29  // Parse multi array.
30  mHashAlgorithm = elem.right(elem.length() - 2).split(QLatin1Char(':'));
31  } else if (elem.startsWith(QLatin1String("k="))) { // Key type (rsa by default)
32  mKeyType = elem.right(elem.length() - 2);
33  } else if (elem.startsWith(QLatin1String("n="))) { // Notes (optional empty by default)
34  mNote = elem.right(elem.length() - 2);
35  } else if (elem.startsWith(QLatin1String("p="))) { // Public key
36  mPublicKey = elem.right(elem.length() - 2).remove(QLatin1Char(' '));
37  } else if (elem.startsWith(QLatin1String("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 == QLatin1String("email")) {
46  mService = service;
47  }
48  }
49  } else if (elem.startsWith(QLatin1String("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 
79 QString DKIMKeyRecord::version() const
80 {
81  return mVersion;
82 }
83 
84 void DKIMKeyRecord::setVersion(const QString &version)
85 {
86  mVersion = version;
87 }
88 
89 QString DKIMKeyRecord::keyType() const
90 {
91  return mKeyType;
92 }
93 
94 void DKIMKeyRecord::setKeyType(const QString &keyType)
95 {
96  mKeyType = keyType;
97 }
98 
99 QString DKIMKeyRecord::note() const
100 {
101  return mNote;
102 }
103 
104 void DKIMKeyRecord::setNote(const QString &note)
105 {
106  mNote = note;
107 }
108 
109 QString DKIMKeyRecord::publicKey() const
110 {
111  return mPublicKey;
112 }
113 
114 void DKIMKeyRecord::setPublicKey(const QString &publicKey)
115 {
116  mPublicKey = publicKey;
117 }
118 
119 QString DKIMKeyRecord::service() const
120 {
121  return mService;
122 }
123 
124 void DKIMKeyRecord::setService(const QString &service)
125 {
126  mService = service;
127 }
128 
129 QStringList DKIMKeyRecord::flags() const
130 {
131  return mFlags;
132 }
133 
134 void DKIMKeyRecord::setFlags(const QStringList &flags)
135 {
136  mFlags = flags;
137 }
138 
139 bool 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 
145 QStringList DKIMKeyRecord::hashAlgorithm() const
146 {
147  return mHashAlgorithm;
148 }
149 
150 void 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 }
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
The DKIMKeyRecord class.
Definition: dkimkeyrecord.h:19
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int count(const T &value) const const
bool isEmpty() const const
int length() const const
const T & at(int i) const const
QString & replace(int position, int n, QChar after)
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
KDB_EXPORT KDbVersionInfo version()
QString right(int n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Dec 3 2023 03:57:06 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.