pdfsignatureutils.cpp 7.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/***************************************************************************
 *   Copyright (C) 2018 by Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com>  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "pdfsignatureutils.h"

#include <KLocalizedString>
#include <QDebug>

#ifdef HAVE_POPPLER_0_73

Albert Astals Cid's avatar
Albert Astals Cid committed
17
18
PopplerCertificateInfo::PopplerCertificateInfo(const Poppler::CertificateInfo &info)
    : m_info(info)
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
}

PopplerCertificateInfo::~PopplerCertificateInfo()
{
}

bool PopplerCertificateInfo::isNull() const
{
    return m_info.isNull();
}

int PopplerCertificateInfo::version() const
{
    return m_info.version();
}

QByteArray PopplerCertificateInfo::serialNumber() const
{
    return m_info.serialNumber();
}

Albert Astals Cid's avatar
Albert Astals Cid committed
41
QString PopplerCertificateInfo::issuerInfo(PopplerCertificateInfo::EntityInfoKey key) const
42
{
Albert Astals Cid's avatar
Albert Astals Cid committed
43
    QString str = m_info.issuerInfo(static_cast<Poppler::CertificateInfo::EntityInfoKey>(key));
44
45
46
    return !str.isEmpty() ? str : i18n("Not Available");
}

Albert Astals Cid's avatar
Albert Astals Cid committed
47
QString PopplerCertificateInfo::subjectInfo(PopplerCertificateInfo::EntityInfoKey key) const
48
{
Albert Astals Cid's avatar
Albert Astals Cid committed
49
    QString str = m_info.subjectInfo(static_cast<Poppler::CertificateInfo::EntityInfoKey>(key));
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    return !str.isEmpty() ? str : i18n("Not Available");
}

QDateTime PopplerCertificateInfo::validityStart() const
{
    return m_info.validityStart();
}

QDateTime PopplerCertificateInfo::validityEnd() const
{
    return m_info.validityEnd();
}

PopplerCertificateInfo::KeyUsageExtensions PopplerCertificateInfo::keyUsageExtensions() const
{
    Poppler::CertificateInfo::KeyUsageExtensions popplerKu = m_info.keyUsageExtensions();
    KeyUsageExtensions ku = KuNone;
Albert Astals Cid's avatar
Albert Astals Cid committed
67
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuDigitalSignature))
68
        ku |= KuDigitalSignature;
Albert Astals Cid's avatar
Albert Astals Cid committed
69
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuNonRepudiation))
70
        ku |= KuNonRepudiation;
Albert Astals Cid's avatar
Albert Astals Cid committed
71
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuKeyEncipherment))
72
        ku |= KuKeyEncipherment;
Albert Astals Cid's avatar
Albert Astals Cid committed
73
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuDataEncipherment))
74
        ku |= KuDataEncipherment;
Albert Astals Cid's avatar
Albert Astals Cid committed
75
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuKeyAgreement))
76
        ku |= KuKeyAgreement;
Albert Astals Cid's avatar
Albert Astals Cid committed
77
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuKeyCertSign))
78
        ku |= KuKeyCertSign;
Albert Astals Cid's avatar
Albert Astals Cid committed
79
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuClrSign))
80
        ku |= KuClrSign;
Albert Astals Cid's avatar
Albert Astals Cid committed
81
    if (popplerKu.testFlag(Poppler::CertificateInfo::KuEncipherOnly))
82
83
84
85
86
87
88
89
90
91
92
        ku |= KuEncipherOnly;
    return ku;
}

QByteArray PopplerCertificateInfo::publicKey() const
{
    return m_info.publicKey();
}

PopplerCertificateInfo::PublicKeyType PopplerCertificateInfo::publicKeyType() const
{
Albert Astals Cid's avatar
Albert Astals Cid committed
93
94
95
96
97
98
99
100
101
    switch (m_info.publicKeyType()) {
    case Poppler::CertificateInfo::RsaKey:
        return RsaKey;
    case Poppler::CertificateInfo::DsaKey:
        return DsaKey;
    case Poppler::CertificateInfo::EcKey:
        return EcKey;
    case Poppler::CertificateInfo::OtherKey:
        return OtherKey;
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
    }

    return OtherKey;
}

int PopplerCertificateInfo::publicKeyStrength() const
{
    return m_info.publicKeyStrength();
}

bool PopplerCertificateInfo::isSelfSigned() const
{
    return m_info.isSelfSigned();
}

QByteArray PopplerCertificateInfo::certificateData() const
{
    return m_info.certificateData();
}

#else

class DummyCertificateInfo : public Okular::CertificateInfo
{
};

#endif

Albert Astals Cid's avatar
Albert Astals Cid committed
130
131
PopplerSignatureInfo::PopplerSignatureInfo(const Poppler::SignatureValidationInfo &info)
    : m_info(info)
132
133
{
#ifdef HAVE_POPPLER_0_73
Albert Astals Cid's avatar
Albert Astals Cid committed
134
    m_certfiticateInfo = new PopplerCertificateInfo(m_info.certificateInfo());
135
136
137
138
139
140
141
142
143
144
145
146
#else
    m_certfiticateInfo = new DummyCertificateInfo();
#endif
}

PopplerSignatureInfo::~PopplerSignatureInfo()
{
    delete m_certfiticateInfo;
}

PopplerSignatureInfo::SignatureStatus PopplerSignatureInfo::signatureStatus() const
{
Albert Astals Cid's avatar
Albert Astals Cid committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
    switch (m_info.signatureStatus()) {
    case Poppler::SignatureValidationInfo::SignatureValid:
        return SignatureValid;
    case Poppler::SignatureValidationInfo::SignatureInvalid:
        return SignatureInvalid;
    case Poppler::SignatureValidationInfo::SignatureDigestMismatch:
        return SignatureDigestMismatch;
    case Poppler::SignatureValidationInfo::SignatureDecodingError:
        return SignatureDecodingError;
    case Poppler::SignatureValidationInfo::SignatureGenericError:
        return SignatureGenericError;
    case Poppler::SignatureValidationInfo::SignatureNotFound:
        return SignatureNotFound;
    case Poppler::SignatureValidationInfo::SignatureNotVerified:
        return SignatureNotVerified;
    default:
        return SignatureStatusUnknown;
164
165
166
167
168
    }
}

PopplerSignatureInfo::CertificateStatus PopplerSignatureInfo::certificateStatus() const
{
Albert Astals Cid's avatar
Albert Astals Cid committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    switch (m_info.certificateStatus()) {
    case Poppler::SignatureValidationInfo::CertificateTrusted:
        return CertificateTrusted;
    case Poppler::SignatureValidationInfo::CertificateUntrustedIssuer:
        return CertificateUntrustedIssuer;
    case Poppler::SignatureValidationInfo::CertificateUnknownIssuer:
        return CertificateUnknownIssuer;
    case Poppler::SignatureValidationInfo::CertificateRevoked:
        return CertificateRevoked;
    case Poppler::SignatureValidationInfo::CertificateExpired:
        return CertificateExpired;
    case Poppler::SignatureValidationInfo::CertificateGenericError:
        return CertificateGenericError;
    case Poppler::SignatureValidationInfo::CertificateNotVerified:
        return CertificateNotVerified;
    default:
        return CertificateStatusUnknown;
186
187
188
189
190
    }
}

PopplerSignatureInfo::HashAlgorithm PopplerSignatureInfo::hashAlgorithm() const
{
Albert Astals Cid's avatar
Albert Astals Cid committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    switch (m_info.hashAlgorithm()) {
    case Poppler::SignatureValidationInfo::HashAlgorithmMd2:
        return HashAlgorithmMd2;
    case Poppler::SignatureValidationInfo::HashAlgorithmMd5:
        return HashAlgorithmMd5;
    case Poppler::SignatureValidationInfo::HashAlgorithmSha1:
        return HashAlgorithmSha1;
    case Poppler::SignatureValidationInfo::HashAlgorithmSha256:
        return HashAlgorithmSha256;
    case Poppler::SignatureValidationInfo::HashAlgorithmSha384:
        return HashAlgorithmSha384;
    case Poppler::SignatureValidationInfo::HashAlgorithmSha512:
        return HashAlgorithmSha512;
    case Poppler::SignatureValidationInfo::HashAlgorithmSha224:
        return HashAlgorithmSha224;
    default:
        return HashAlgorithmUnknown;
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    }
}

QString PopplerSignatureInfo::signerName() const
{
    return m_info.signerName();
}

QString PopplerSignatureInfo::signerSubjectDN() const
{
    return m_info.signerSubjectDN();
}

QString PopplerSignatureInfo::location() const
{
    return m_info.location();
}

QString PopplerSignatureInfo::reason() const
{
    return m_info.reason();
}

QDateTime PopplerSignatureInfo::signingTime() const
{
Albert Astals Cid's avatar
Albert Astals Cid committed
233
    return QDateTime::fromSecsSinceEpoch(m_info.signingTime());
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
}

QByteArray PopplerSignatureInfo::signature() const
{
    return m_info.signature();
}

QList<qint64> PopplerSignatureInfo::signedRangeBounds() const
{
    return m_info.signedRangeBounds();
}

bool PopplerSignatureInfo::signsTotalDocument() const
{
    return m_info.signsTotalDocument();
}

const Okular::CertificateInfo &PopplerSignatureInfo::certificateInfo() const
{
    return *m_certfiticateInfo;
}