Okular

signatureutils.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "signatureutils.h"
8#include <KLocalizedString>
9
10using namespace Okular;
11
12static QString handleEmpty(const QString &string, CertificateInfo::EmptyString empty)
13{
14 if (string.isEmpty()) {
15 switch (empty) {
16 case CertificateInfo::EmptyString::Empty:
17 return {};
18 case CertificateInfo::EmptyString::TranslatedNotAvailable:
19 return i18n("Not Available");
20 }
21 return {};
22 }
23 return string;
24}
25
26class EntityInfo
27{
28public:
29 QString commonName;
30 QString distinguishedName;
31 QString emailAddress;
32 QString organization;
33};
34class Okular::CertificateInfoPrivate : public QSharedData
35{
36public:
37 bool isNull = true;
38 int version = -1;
39 QByteArray serialNumber;
40 EntityInfo issuerInfo;
41 EntityInfo subjectInfo;
42 QString nickName;
43 QDateTime validityStart;
44 QDateTime validityEnd;
45 CertificateInfo::KeyUsageExtensions keyUsageExtensions = CertificateInfo::KuNone;
46 QByteArray publicKey;
47 CertificateInfo::PublicKeyType publicKeyType = CertificateInfo::OtherKey;
48 int publicKeyStrength = -1;
49 bool isSelfSigned = false;
50 QByteArray certificateData;
51 CertificateInfo::Backend backend = CertificateInfo::Backend::Unknown;
52 CertificateInfo::KeyLocation keyLocation = CertificateInfo::KeyLocation::Unknown;
53 bool isQualified = false;
54 std::function<bool(QString)> checkPasswordFunction;
55};
56
57CertificateInfo::CertificateInfo()
58 : d {new CertificateInfoPrivate()}
59{
60}
61
62Okular::CertificateInfo::CertificateInfo(const Okular::CertificateInfo &other) = default;
63Okular::CertificateInfo::CertificateInfo(Okular::CertificateInfo &&other) noexcept = default;
64Okular::CertificateInfo &Okular::CertificateInfo::operator=(const Okular::CertificateInfo &other) = default;
65CertificateInfo &Okular::CertificateInfo::operator=(Okular::CertificateInfo &&other) noexcept = default;
66
67CertificateInfo::~CertificateInfo() = default;
68
69Q_DECLARE_OPERATORS_FOR_FLAGS(CertificateInfo::KeyUsageExtensions)
70
71bool CertificateInfo::isNull() const
72{
73 return d->isNull;
74}
75
76void CertificateInfo::setNull(bool isNull)
77{
78 d->isNull = isNull;
79}
80
81int CertificateInfo::version() const
82{
83 return d->version;
84}
85void CertificateInfo::setVersion(int version)
86{
87 d->version = version;
88}
89
90QByteArray CertificateInfo::serialNumber() const
91{
92 return d->serialNumber;
93}
94void CertificateInfo::setSerialNumber(const QByteArray &serialNumber)
95{
96 d->serialNumber = serialNumber;
97}
98
99QString CertificateInfo::issuerInfo(EntityInfoKey key, EmptyString empty) const
100{
101 switch (key) {
102 case EntityInfoKey::CommonName:
103 return handleEmpty(d->issuerInfo.commonName, empty);
104 case EntityInfoKey::DistinguishedName:
105 return handleEmpty(d->issuerInfo.distinguishedName, empty);
106 case EntityInfoKey::EmailAddress:
107 return handleEmpty(d->issuerInfo.emailAddress, empty);
108 case EntityInfoKey::Organization:
109 return handleEmpty(d->issuerInfo.organization, empty);
110 }
111 return QString();
112}
113
114void CertificateInfo::setIssuerInfo(EntityInfoKey key, const QString &value)
115{
116 switch (key) {
117 case EntityInfoKey::CommonName:
118 d->issuerInfo.commonName = value;
119 return;
120 case EntityInfoKey::DistinguishedName:
121 d->issuerInfo.distinguishedName = value;
122 return;
123 case EntityInfoKey::EmailAddress:
124 d->issuerInfo.emailAddress = value;
125 return;
126 case EntityInfoKey::Organization:
127 d->issuerInfo.organization = value;
128 return;
129 }
130}
131
132QString CertificateInfo::subjectInfo(EntityInfoKey key, EmptyString empty) const
133{
134 switch (key) {
135 case EntityInfoKey::CommonName:
136 return handleEmpty(d->subjectInfo.commonName, empty);
137 case EntityInfoKey::DistinguishedName:
138 return handleEmpty(d->subjectInfo.distinguishedName, empty);
139 case EntityInfoKey::EmailAddress:
140 return handleEmpty(d->subjectInfo.emailAddress, empty);
141 case EntityInfoKey::Organization:
142 return handleEmpty(d->subjectInfo.organization, empty);
143 }
144 return QString();
145}
146
147void CertificateInfo::setSubjectInfo(EntityInfoKey key, const QString &value)
148{
149 switch (key) {
150 case EntityInfoKey::CommonName:
151 d->subjectInfo.commonName = value;
152 return;
153 case EntityInfoKey::DistinguishedName:
154 d->subjectInfo.distinguishedName = value;
155 return;
156 case EntityInfoKey::EmailAddress:
157 d->subjectInfo.emailAddress = value;
158 return;
159 case EntityInfoKey::Organization:
160 d->subjectInfo.organization = value;
161 return;
162 }
163}
164
165QString CertificateInfo::nickName() const
166{
167 return d->nickName;
168}
169
170void CertificateInfo::setNickName(const QString &nickName)
171{
172 d->nickName = nickName;
173}
174
175QDateTime CertificateInfo::validityStart() const
176{
177 return d->validityStart;
178}
179
180void CertificateInfo::setValidityStart(const QDateTime &start)
181{
182 d->validityStart = start;
183}
184
185QDateTime CertificateInfo::validityEnd() const
186{
187 return d->validityEnd;
188}
189
190void Okular::CertificateInfo::setValidityEnd(const QDateTime &validityEnd)
191{
192 d->validityEnd = validityEnd;
193}
194
195CertificateInfo::KeyUsageExtensions CertificateInfo::keyUsageExtensions() const
196{
197 return d->keyUsageExtensions;
198}
199
200void Okular::CertificateInfo::setKeyUsageExtensions(Okular::CertificateInfo::KeyUsageExtensions ext)
201{
202 d->keyUsageExtensions = ext;
203}
204
205QByteArray CertificateInfo::publicKey() const
206{
207 return d->publicKey;
208}
209
210void Okular::CertificateInfo::setPublicKey(const QByteArray &publicKey)
211{
212 d->publicKey = publicKey;
213}
214
215CertificateInfo::PublicKeyType CertificateInfo::publicKeyType() const
216{
217 return d->publicKeyType;
218}
219
220void CertificateInfo::setPublicKeyType(PublicKeyType type)
221{
222 d->publicKeyType = type;
223}
224
225int CertificateInfo::publicKeyStrength() const
226{
227 return d->publicKeyStrength;
228}
229
230void CertificateInfo::setPublicKeyStrength(int strength)
231{
232 d->publicKeyStrength = strength;
233}
234
235bool CertificateInfo::isSelfSigned() const
236{
237 return d->isSelfSigned;
238}
239
240void CertificateInfo::setSelfSigned(bool selfSigned)
241{
242 d->isSelfSigned = selfSigned;
243}
244
245QByteArray CertificateInfo::certificateData() const
246{
247 return d->certificateData;
248}
249
250void CertificateInfo::setCertificateData(const QByteArray &certificateData)
251{
252 d->certificateData = certificateData;
253}
254
255CertificateInfo::KeyLocation CertificateInfo::keyLocation() const
256{
257 return d->keyLocation;
258}
259
260void CertificateInfo::setKeyLocation(KeyLocation location)
261{
262 d->keyLocation = location;
263}
264
265CertificateInfo::Backend CertificateInfo::backend() const
266{
267 return d->backend;
268}
269
270void CertificateInfo::setBackend(Backend backend)
271{
272 d->backend = backend;
273}
274
275bool CertificateInfo::checkPassword(const QString &password) const
276{
277 if (d->checkPasswordFunction) {
278 return d->checkPasswordFunction(password);
279 }
280 return false;
281}
282
283void CertificateInfo::setCheckPasswordFunction(const std::function<bool(const QString &)> &passwordFunction)
284{
285 d->checkPasswordFunction = passwordFunction;
286}
287
288bool CertificateInfo::isQualified() const
289{
290 return d->isQualified;
291}
292
293void CertificateInfo::setQualified(bool qualified)
294{
295 d->isQualified = qualified;
296}
297
298class Okular::SignatureInfoPrivate : public QSharedData
299{
300public:
301 SignatureInfo::SignatureStatus signatureStatus = SignatureInfo::SignatureStatusUnknown;
302 SignatureInfo::CertificateStatus certificateStatus = SignatureInfo::CertificateStatusUnknown;
303 SignatureInfo::HashAlgorithm hashAlgorithm = SignatureInfo::HashAlgorithmUnknown;
304 QString signerName;
305 QString signerSubjectDN;
307 QString reason;
308 QDateTime signingTime;
309 QByteArray signature;
310 QList<qint64> signedRangeBounds;
311 bool signsTotalDocument = false;
312 CertificateInfo certificateInfo;
313};
314
315SignatureInfo::SignatureInfo()
316 : d {new SignatureInfoPrivate()}
317{
318}
319
320SignatureInfo::SignatureInfo(SignatureInfo &&other) noexcept = default;
321SignatureInfo::SignatureInfo(const SignatureInfo &other) = default;
322SignatureInfo &SignatureInfo::operator=(SignatureInfo &&other) noexcept = default;
323Okular::SignatureInfo &Okular::SignatureInfo::operator=(const Okular::SignatureInfo &other) = default;
324SignatureInfo::~SignatureInfo() = default;
325
326SignatureInfo::SignatureStatus SignatureInfo::signatureStatus() const
327{
328 return d->signatureStatus;
329}
330
331void SignatureInfo::setSignatureStatus(SignatureInfo::SignatureStatus status)
332{
333 d->signatureStatus = status;
334}
335
336SignatureInfo::CertificateStatus SignatureInfo::certificateStatus() const
337{
338 return d->certificateStatus;
339}
340
341void SignatureInfo::setCertificateStatus(SignatureInfo::CertificateStatus status)
342{
343 d->certificateStatus = status;
344}
345
346SignatureInfo::HashAlgorithm SignatureInfo::hashAlgorithm() const
347{
348 return d->hashAlgorithm;
349}
350
351void Okular::SignatureInfo::setHashAlgorithm(Okular::SignatureInfo::HashAlgorithm algorithm)
352{
353 d->hashAlgorithm = algorithm;
354}
355
356QString SignatureInfo::signerName() const
357{
358 return d->signerName;
359}
360
361void SignatureInfo::setSignerName(const QString &signerName)
362{
363 d->signerName = signerName;
364}
365
366QString SignatureInfo::signerSubjectDN() const
367{
368 return d->signerSubjectDN;
369}
370
371void Okular::SignatureInfo::setSignerSubjectDN(const QString &signerSubjectDN)
372{
373 d->signerSubjectDN = signerSubjectDN;
374}
375
376QString SignatureInfo::location() const
377{
378 return d->location;
379}
380
381void SignatureInfo::setLocation(const QString &location)
382{
383 d->location = location;
384}
385
386QString SignatureInfo::reason() const
387{
388 return d->reason;
389}
390
391void Okular::SignatureInfo::setReason(const QString &reason)
392{
393 d->reason = reason;
394}
395
396QDateTime SignatureInfo::signingTime() const
397{
398 return d->signingTime;
399}
400
401void Okular::SignatureInfo::setSigningTime(const QDateTime &time)
402{
403 d->signingTime = time;
404}
405
406QByteArray SignatureInfo::signature() const
407{
408 return d->signature;
409}
410
411void SignatureInfo::setSignature(const QByteArray &signature)
412{
413 d->signature = signature;
414}
415
416QList<qint64> SignatureInfo::signedRangeBounds() const
417{
418 return d->signedRangeBounds;
419}
420
421void SignatureInfo::setSignedRangeBounds(const QList<qint64> &range)
422{
423 d->signedRangeBounds = range;
424}
425
426bool SignatureInfo::signsTotalDocument() const
427{
428 return d->signsTotalDocument;
429}
430
431void SignatureInfo::setSignsTotalDocument(bool total)
432{
433 d->signsTotalDocument = total;
434}
435
436CertificateInfo SignatureInfo::certificateInfo() const
437{
438 return d->certificateInfo;
439}
440
441void SignatureInfo::setCertificateInfo(const Okular::CertificateInfo &info)
442{
443 d->certificateInfo = info;
444}
445
446CertificateStore::CertificateStore()
447{
448}
449
453
455{
456 *userCancelled = false;
457 return QList<CertificateInfo>();
458}
459
460QList<CertificateInfo> CertificateStore::signingCertificatesForNow(bool *userCancelled, bool *nonDateValidCerts) const
461{
464 auto it = certs.begin();
465 *nonDateValidCerts = false;
466 while (it != certs.end()) {
467 if (it->validityStart() > now || now > it->validityEnd()) {
468 it = certs.erase(it);
469 *nonDateValidCerts = true;
470 } else {
471 ++it;
472 }
473 }
474 return certs;
475}
QList< CertificateInfo > signingCertificatesForNow(bool *userCancelled, bool *nonDateValidCerts) const
Returns list of valid, usable signing certificates for current date and time.
virtual ~CertificateStore()
Destructor.
virtual QList< CertificateInfo > signingCertificates(bool *userCancelled) const
Returns list of valid, usable signing certificates.
Q_SCRIPTABLE CaptureState status()
Q_SCRIPTABLE Q_NOREPLY void start()
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
KCOREADDONS_EXPORT unsigned int version()
QVariant location(const QVariant &res)
global.h
Definition action.h:17
QDateTime currentDateTime()
iterator begin()
iterator end()
iterator erase(const_iterator begin, const_iterator end)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.