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 std::function<bool(QString)> checkPasswordFunction;
54};
55
56CertificateInfo::CertificateInfo()
57 : d {new CertificateInfoPrivate()}
58{
59}
60
61Okular::CertificateInfo::CertificateInfo(const Okular::CertificateInfo &other) = default;
62Okular::CertificateInfo::CertificateInfo(Okular::CertificateInfo &&other) noexcept = default;
63Okular::CertificateInfo &Okular::CertificateInfo::operator=(const Okular::CertificateInfo &other) = default;
64CertificateInfo &Okular::CertificateInfo::operator=(Okular::CertificateInfo &&other) noexcept = default;
65
66CertificateInfo::~CertificateInfo() = default;
67
68Q_DECLARE_OPERATORS_FOR_FLAGS(CertificateInfo::KeyUsageExtensions)
69
70bool CertificateInfo::isNull() const
71{
72 return d->isNull;
73}
74
75void CertificateInfo::setNull(bool isNull)
76{
77 d->isNull = isNull;
78}
79
80int CertificateInfo::version() const
81{
82 return d->version;
83}
84void CertificateInfo::setVersion(int version)
85{
86 d->version = version;
87}
88
89QByteArray CertificateInfo::serialNumber() const
90{
91 return d->serialNumber;
92}
93void CertificateInfo::setSerialNumber(const QByteArray &serialNumber)
94{
95 d->serialNumber = serialNumber;
96}
97
98QString CertificateInfo::issuerInfo(EntityInfoKey key, EmptyString empty) const
99{
100 switch (key) {
101 case EntityInfoKey::CommonName:
102 return handleEmpty(d->issuerInfo.commonName, empty);
103 case EntityInfoKey::DistinguishedName:
104 return handleEmpty(d->issuerInfo.distinguishedName, empty);
105 case EntityInfoKey::EmailAddress:
106 return handleEmpty(d->issuerInfo.emailAddress, empty);
107 case EntityInfoKey::Organization:
108 return handleEmpty(d->issuerInfo.organization, empty);
109 }
110 return QString();
111}
112
113void CertificateInfo::setIssuerInfo(EntityInfoKey key, const QString &value)
114{
115 switch (key) {
116 case EntityInfoKey::CommonName:
117 d->issuerInfo.commonName = value;
118 return;
119 case EntityInfoKey::DistinguishedName:
120 d->issuerInfo.distinguishedName = value;
121 return;
122 case EntityInfoKey::EmailAddress:
123 d->issuerInfo.emailAddress = value;
124 return;
125 case EntityInfoKey::Organization:
126 d->issuerInfo.organization = value;
127 return;
128 }
129}
130
131QString CertificateInfo::subjectInfo(EntityInfoKey key, EmptyString empty) const
132{
133 switch (key) {
134 case EntityInfoKey::CommonName:
135 return handleEmpty(d->subjectInfo.commonName, empty);
136 case EntityInfoKey::DistinguishedName:
137 return handleEmpty(d->subjectInfo.distinguishedName, empty);
138 case EntityInfoKey::EmailAddress:
139 return handleEmpty(d->subjectInfo.emailAddress, empty);
140 case EntityInfoKey::Organization:
141 return handleEmpty(d->subjectInfo.organization, empty);
142 }
143 return QString();
144}
145
146void CertificateInfo::setSubjectInfo(EntityInfoKey key, const QString &value)
147{
148 switch (key) {
149 case EntityInfoKey::CommonName:
150 d->subjectInfo.commonName = value;
151 return;
152 case EntityInfoKey::DistinguishedName:
153 d->subjectInfo.distinguishedName = value;
154 return;
155 case EntityInfoKey::EmailAddress:
156 d->subjectInfo.emailAddress = value;
157 return;
158 case EntityInfoKey::Organization:
159 d->subjectInfo.organization = value;
160 return;
161 }
162}
163
164QString CertificateInfo::nickName() const
165{
166 return d->nickName;
167}
168
169void CertificateInfo::setNickName(const QString &nickName)
170{
171 d->nickName = nickName;
172}
173
174QDateTime CertificateInfo::validityStart() const
175{
176 return d->validityStart;
177}
178
179void CertificateInfo::setValidityStart(const QDateTime &start)
180{
181 d->validityStart = start;
182}
183
184QDateTime CertificateInfo::validityEnd() const
185{
186 return d->validityEnd;
187}
188
189void Okular::CertificateInfo::setValidityEnd(const QDateTime &validityEnd)
190{
191 d->validityEnd = validityEnd;
192}
193
194CertificateInfo::KeyUsageExtensions CertificateInfo::keyUsageExtensions() const
195{
196 return d->keyUsageExtensions;
197}
198
199void Okular::CertificateInfo::setKeyUsageExtensions(Okular::CertificateInfo::KeyUsageExtensions ext)
200{
201 d->keyUsageExtensions = ext;
202}
203
204QByteArray CertificateInfo::publicKey() const
205{
206 return d->publicKey;
207}
208
209void Okular::CertificateInfo::setPublicKey(const QByteArray &publicKey)
210{
211 d->publicKey = publicKey;
212}
213
214CertificateInfo::PublicKeyType CertificateInfo::publicKeyType() const
215{
216 return d->publicKeyType;
217}
218
219void CertificateInfo::setPublicKeyType(PublicKeyType type)
220{
221 d->publicKeyType = type;
222}
223
224int CertificateInfo::publicKeyStrength() const
225{
226 return d->publicKeyStrength;
227}
228
229void CertificateInfo::setPublicKeyStrength(int strength)
230{
231 d->publicKeyStrength = strength;
232}
233
234bool CertificateInfo::isSelfSigned() const
235{
236 return d->isSelfSigned;
237}
238
239void CertificateInfo::setSelfSigned(bool selfSigned)
240{
241 d->isSelfSigned = selfSigned;
242}
243
244QByteArray CertificateInfo::certificateData() const
245{
246 return d->certificateData;
247}
248
249void CertificateInfo::setCertificateData(const QByteArray &certificateData)
250{
251 d->certificateData = certificateData;
252}
253
254CertificateInfo::KeyLocation CertificateInfo::keyLocation() const
255{
256 return d->keyLocation;
257}
258
259void CertificateInfo::setKeyLocation(KeyLocation location)
260{
261 d->keyLocation = location;
262}
263
264CertificateInfo::Backend CertificateInfo::backend() const
265{
266 return d->backend;
267}
268
269void CertificateInfo::setBackend(Backend backend)
270{
271 d->backend = backend;
272}
273
274bool CertificateInfo::checkPassword(const QString &password) const
275{
276 if (d->checkPasswordFunction) {
277 return d->checkPasswordFunction(password);
278 }
279 return false;
280}
281
282void CertificateInfo::setCheckPasswordFunction(const std::function<bool(const QString &)> &passwordFunction)
283{
284 d->checkPasswordFunction = passwordFunction;
285}
286
287class Okular::SignatureInfoPrivate : public QSharedData
288{
289public:
290 SignatureInfo::SignatureStatus signatureStatus = SignatureInfo::SignatureStatusUnknown;
291 SignatureInfo::CertificateStatus certificateStatus = SignatureInfo::CertificateStatusUnknown;
292 SignatureInfo::HashAlgorithm hashAlgorithm = SignatureInfo::HashAlgorithmUnknown;
293 QString signerName;
294 QString signerSubjectDN;
296 QString reason;
297 QDateTime signingTime;
298 QByteArray signature;
299 QList<qint64> signedRangeBounds;
300 bool signsTotalDocument = false;
301 CertificateInfo certificateInfo;
302};
303
304SignatureInfo::SignatureInfo()
305 : d {new SignatureInfoPrivate()}
306{
307}
308
309SignatureInfo::SignatureInfo(SignatureInfo &&other) noexcept = default;
310SignatureInfo::SignatureInfo(const SignatureInfo &other) = default;
311SignatureInfo &SignatureInfo::operator=(SignatureInfo &&other) noexcept = default;
312Okular::SignatureInfo &Okular::SignatureInfo::operator=(const Okular::SignatureInfo &other) = default;
313SignatureInfo::~SignatureInfo() = default;
314
315SignatureInfo::SignatureStatus SignatureInfo::signatureStatus() const
316{
317 return d->signatureStatus;
318}
319
320void SignatureInfo::setSignatureStatus(SignatureInfo::SignatureStatus status)
321{
322 d->signatureStatus = status;
323}
324
325SignatureInfo::CertificateStatus SignatureInfo::certificateStatus() const
326{
327 return d->certificateStatus;
328}
329
330void SignatureInfo::setCertificateStatus(SignatureInfo::CertificateStatus status)
331{
332 d->certificateStatus = status;
333}
334
335SignatureInfo::HashAlgorithm SignatureInfo::hashAlgorithm() const
336{
337 return d->hashAlgorithm;
338}
339
340void Okular::SignatureInfo::setHashAlgorithm(Okular::SignatureInfo::HashAlgorithm algorithm)
341{
342 d->hashAlgorithm = algorithm;
343}
344
345QString SignatureInfo::signerName() const
346{
347 return d->signerName;
348}
349
350void SignatureInfo::setSignerName(const QString &signerName)
351{
352 d->signerName = signerName;
353}
354
355QString SignatureInfo::signerSubjectDN() const
356{
357 return d->signerSubjectDN;
358}
359
360void Okular::SignatureInfo::setSignerSubjectDN(const QString &signerSubjectDN)
361{
362 d->signerSubjectDN = signerSubjectDN;
363}
364
365QString SignatureInfo::location() const
366{
367 return d->location;
368}
369
370void SignatureInfo::setLocation(const QString &location)
371{
372 d->location = location;
373}
374
375QString SignatureInfo::reason() const
376{
377 return d->reason;
378}
379
380void Okular::SignatureInfo::setReason(const QString &reason)
381{
382 d->reason = reason;
383}
384
385QDateTime SignatureInfo::signingTime() const
386{
387 return d->signingTime;
388}
389
390void Okular::SignatureInfo::setSigningTime(const QDateTime &time)
391{
392 d->signingTime = time;
393}
394
395QByteArray SignatureInfo::signature() const
396{
397 return d->signature;
398}
399
400void SignatureInfo::setSignature(const QByteArray &signature)
401{
402 d->signature = signature;
403}
404
405QList<qint64> SignatureInfo::signedRangeBounds() const
406{
407 return d->signedRangeBounds;
408}
409
410void SignatureInfo::setSignedRangeBounds(const QList<qint64> &range)
411{
412 d->signedRangeBounds = range;
413}
414
415bool SignatureInfo::signsTotalDocument() const
416{
417 return d->signsTotalDocument;
418}
419
420void SignatureInfo::setSignsTotalDocument(bool total)
421{
422 d->signsTotalDocument = total;
423}
424
425CertificateInfo SignatureInfo::certificateInfo() const
426{
427 return d->certificateInfo;
428}
429
430void SignatureInfo::setCertificateInfo(const Okular::CertificateInfo &info)
431{
432 d->certificateInfo = info;
433}
434
435CertificateStore::CertificateStore()
436{
437}
438
442
444{
445 *userCancelled = false;
446 return QList<CertificateInfo>();
447}
448
449QList<CertificateInfo> CertificateStore::signingCertificatesForNow(bool *userCancelled, bool *nonDateValidCerts) const
450{
453 auto it = certs.begin();
454 *nonDateValidCerts = false;
455 while (it != certs.end()) {
456 if (it->validityStart() > now || now > it->validityEnd()) {
457 it = certs.erase(it);
458 *nonDateValidCerts = true;
459 } else {
460 ++it;
461 }
462 }
463 return certs;
464}
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)
KDB_EXPORT KDbVersionInfo 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 Mon Nov 18 2024 12:14:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.