Messagelib

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

KDE's Doxygen guidelines are available online.