Skip to content

Latest commit

 

History

History
800 lines (677 loc) · 25.1 KB

certificatemodel.cpp

File metadata and controls

800 lines (677 loc) · 25.1 KB
 
Sep 20, 2019
Sep 20, 2019
2
3
* Copyright (c) 2016 - 2019 Jolla Ltd.
* Copyright (c) 2019 Open Mobile Platform LLC.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
*
* You may use this file under the terms of the BSD license as follows:
*
* "Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Nemo Mobile nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
#include "certificatemodel.h"
#include <QFile>
#include <QRegularExpression>
#include <QDebug>
Feb 21, 2019
Feb 21, 2019
38
#include <functional>
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <openssl/opensslv.h>
#include <openssl/bio.h>
#include <openssl/conf.h>
#include <openssl/engine.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509v3.h>
namespace {
struct X509List;
}
struct X509Certificate
{
QList<QPair<QString, QString>> subjectList(bool shortForm = false) const
{
return nameList(X509_get_subject_name(x509), shortForm);
}
QString subject(bool shortForm = true, const QString &separator = QString(", ")) const
{
return toString(subjectList(shortForm), separator);
}
QString subjectElement(int nid) const
{
return nameElement(X509_get_subject_name(x509), nid);
}
QList<QPair<QString, QString>> issuerList(bool shortForm = false) const
{
return nameList(X509_get_issuer_name(x509), shortForm);
}
QString issuer(bool shortForm = true, const QString &separator = QString(", ")) const
{
return toString(issuerList(shortForm), separator);
}
QString issuerElement(int nid) const
{
return nameElement(X509_get_issuer_name(x509), nid);
}
QString version() const
{
return QString::number(X509_get_version(x509) + 1);
}
QString serialNumber() const
{
return integerToString(X509_get_serialNumber(x509));
}
QDateTime notBefore() const
{
return toDateTime(X509_get_notBefore(x509));
}
QDateTime notAfter() const
{
return toDateTime(X509_get_notAfter(x509));
}
QList<QPair<QString, QString>> publicKeyList(bool shortForm = false) const
{
QList<QPair<QString, QString>> rv;
if (EVP_PKEY *key = X509_get_pubkey(x509)) {
rv.append(qMakePair(QStringLiteral("Algorithm"), idToString(EVP_PKEY_id(key), shortForm)));
rv.append(qMakePair(QStringLiteral("Bits"), QString::number(EVP_PKEY_bits(key))));
BIO *b = BIO_new(BIO_s_mem());
EVP_PKEY_print_public(b, key, 0, 0);
const QList<QPair<QString, QString>> &details(parseData(bioToString(b)));
for (auto it = details.cbegin(), end = details.cend(); it != end; ++it) {
rv.append(qMakePair(it->first, it->second));
}
BIO_free(b);
EVP_PKEY_free(key);
}
return rv;
}
QList<QPair<QString, QString>> extensionList(bool shortForm = false) const
{
QList<QPair<QString, QString>> rv;
for (int i = 0, n = sk_X509_EXTENSION_num(x509->cert_info->extensions); i < n; ++i) {
X509_EXTENSION *extension = sk_X509_EXTENSION_value(x509->cert_info->extensions, i);
ASN1_OBJECT *object = X509_EXTENSION_get_object(extension);
int nid = OBJ_obj2nid(object);
if (nid == NID_undef)
continue;
QString name(objectToString(object, shortForm));
if (X509_EXTENSION_get_critical(extension) > 0) {
name.append(QStringLiteral(" (Critical)"));
}
BIO *b = BIO_new(BIO_s_mem());
X509V3_EXT_print(b, extension, 0, 0);
rv.append(qMakePair(name, bioToString(b)));
BIO_free(b);
}
return rv;
}
QList<QPair<QString, QString>> signatureList(bool shortForm = false) const
{
QList<QPair<QString, QString>> rv;
rv.append(qMakePair(QStringLiteral("Algorithm"), objectToString(x509->sig_alg->algorithm, shortForm)));
BIO *b = BIO_new(BIO_s_mem());
X509_signature_dump(b, x509->signature, 0);
QString d(bioToString(b).replace(QChar('\n'), QString()));
rv.append(qMakePair(QStringLiteral("Data"), d.trimmed()));
BIO_free(b);
return rv;
}
private:
static QString stringToString(ASN1_STRING *data)
{
return QString::fromUtf8(reinterpret_cast<char*>(ASN1_STRING_data(data)));
}
static QString timeToString(ASN1_TIME *data)
{
return stringToString(data);
}
static QString idToString(int nid, bool shortForm)
{
return QString::fromUtf8(shortForm ? OBJ_nid2sn(nid) : OBJ_nid2ln(nid));
}
static QString objectToString(ASN1_OBJECT *object, bool shortForm)
{
return idToString(OBJ_obj2nid(object), shortForm);
}
static QString integerToString(ASN1_INTEGER *integer)
{
if (integer->type != V_ASN1_INTEGER && integer->type != V_ASN1_NEG_INTEGER)
return QString();
quint64 value = 0;
for (size_t i = 0, n = qMin(integer->length, 8); i < n; ++i)
value = value << 8 | integer->data[i];
QString rv = QString::number(value);
if (integer->type == V_ASN1_NEG_INTEGER)
rv.prepend(QStringLiteral("-"));
return rv;
}
static QString bioToString(BIO *bio)
{
char *out = 0;
int n = BIO_get_mem_data(bio, &out);
return QString::fromUtf8(QByteArray::fromRawData(out, n));
}
static QList<QPair<QString, QString>> nameList(X509_NAME *name, bool shortForm = true)
{
QList<QPair<QString, QString>> rv;
for (int i = 0, n = X509_NAME_entry_count(name); i < n; ++i) {
X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
ASN1_OBJECT *object = X509_NAME_ENTRY_get_object(entry);
ASN1_STRING *data = X509_NAME_ENTRY_get_data(entry);
rv.append(qMakePair(objectToString(object, shortForm), stringToString(data)));
}
return rv;
}
static QString nameElement(X509_NAME *name, int nid)
{
for (int i = 0, n = X509_NAME_entry_count(name); i < n; ++i) {
X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i);
ASN1_OBJECT *object = X509_NAME_ENTRY_get_object(entry);
if (OBJ_obj2nid(object) == nid) {
ASN1_STRING *data = X509_NAME_ENTRY_get_data(entry);
return stringToString(data);
}
}
return QString();
}
static QString toString(const QList<QPair<QString, QString>> &list, const QString &separator = QString(", "))
{
QString rv;
for (auto it = list.cbegin(), end = list.cend(); it != end; ++it) {
if (!rv.isEmpty()) {
rv.append(separator);
}
rv.append(it->first);
rv.append(QChar(':'));
rv.append(it->second);
}
return rv;
}
static QDateTime toDateTime(ASN1_TIME *time)
{
const QString ts(timeToString(time));
return (time->type == V_ASN1_GENERALIZEDTIME ? fromGENERALIZEDTIME(ts) : fromUTCTIME(ts));
}
static QDateTime fromUTCTIME(const QString &ts)
{
QDate d;
QTime t;
int offset = 0;
// "YYMMDDhhmm[ss](Z|(+|-)hhmm)"
const QRegularExpression re("([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})?(Z)?(([+-])([0-9]{2})([0-9]{2}))?");
QRegularExpressionMatch match = re.match(ts);
if (match.hasMatch()) {
int y = match.captured(1).toInt();
d = QDate((y < 70 ? 2000 : 1900) + y, match.captured(2).toInt(), match.captured(3).toInt());
t = QTime(match.captured(4).toInt(), match.captured(5).toInt(), match.captured(6).toInt());
if (match.lastCapturedIndex() > 7) {
offset = match.captured(11).toInt() * 60 + match.captured(10).toInt() * 60*60;
if (match.captured(9) == "-") {
offset = -offset;
}
}
}
return QDateTime(d, t, Qt::OffsetFromUTC, offset);
}
static QDateTime fromGENERALIZEDTIME(const QString &ts)
{
QDate d;
QTime t;
int offset = 0;
// "YYYYMMDDhh[mm[ss[.fff]]](Z|(+|-)hhmm)" <- nested optionals can be treated as appearing sequentially
const QRegularExpression re("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})?([0-9]{2})?(\\.[0-9]{1,3})?(Z)?(([+-])([0-9]{2})([0-9]{2}))?");
QRegularExpressionMatch match = re.match(ts);
if (match.hasMatch()) {
d = QDate(match.captured(1).toInt(), match.captured(2).toInt(), match.captured(3).toInt());
double fraction = match.captured(7).toDouble();
int ms = (fraction * 1000);
t = QTime(match.captured(4).toInt(), match.captured(5).toInt(), match.captured(6).toInt(), ms);
if (match.lastCapturedIndex() > 8) {
offset = match.captured(12).toInt() * 60 + match.captured(11).toInt() * 60*60;
if (match.captured(10) == "-") {
offset = -offset;
}
}
}
return QDateTime(d, t, Qt::OffsetFromUTC, offset);
}
static QList<QPair<QString, QString>> parseData(QString data)
{
QList<QPair<QString, QString>> rv;
// Join any data with the preceding header
data.replace(QRegularExpression(": *\n +"), QStringLiteral(":"));
foreach (const QString &line, data.split(QString("\n"), QString::SkipEmptyParts)) {
int index = line.indexOf(QChar(':'));
if (index != -1) {
QString name(line.left(index));
QString value(line.mid(index + 1).trimmed());
rv.append(qMakePair(name, value));
}
}
return rv;
}
Aug 17, 2018
Aug 17, 2018
330
friend struct ::X509List;
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
X509Certificate(X509 *x) : x509(x) {}
X509 *x509;
};
namespace {
struct X509List
{
X509List()
: crlStack(0), certificateStack(0), pkcs7(0), pkcs7Signed(0)
{
crlStack = sk_X509_CRL_new_null();
if (!crlStack) {
qWarning() << "Unable to allocate CRL stack";
} else {
certificateStack = sk_X509_new_null();
if (!certificateStack) {
qWarning() << "Unable to allocate X509 stack";
} else {
pkcs7 = PKCS7_new();
pkcs7Signed = PKCS7_SIGNED_new();
if (!pkcs7 || !pkcs7Signed) {
qWarning() << "Unable to create PKCS7 structures";
} else {
pkcs7Signed->crl = crlStack;
pkcs7Signed->cert = certificateStack;
pkcs7->type = OBJ_nid2obj(NID_pkcs7_signed);
pkcs7->d.sign = pkcs7Signed;
pkcs7Signed->contents->type = OBJ_nid2obj(NID_pkcs7_data);
if (!ASN1_INTEGER_set(pkcs7Signed->version, 1)) {
qWarning() << "Unable to set PKCS7 signed version";
}
}
}
}
}
~X509List()
{
/* Apparently, pkcs7Signed and pkcs7 cannot be safely freed...
if (pkcs7Signed)
PKCS7_SIGNED_free(pkcs7Signed);
if (pkcs7)
PKCS7_free(pkcs7);
*/
if (certificateStack)
sk_X509_free(certificateStack);
if (crlStack)
sk_X509_CRL_free(crlStack);
}
bool isValid() const
{
return pkcs7 && pkcs7Signed;
}
int count() const
{
return sk_X509_num(certificateStack);
}
void append(X509 *x509)
{
sk_X509_push(certificateStack, x509);
}
void for_each(std::function<void (const X509Certificate &)> fn) const
{
for (int i = 0, n(count()); i < n; ++i) {
fn(X509Certificate(sk_X509_value(certificateStack, i)));
}
}
private:
STACK_OF(X509_CRL) *crlStack;
STACK_OF(X509) *certificateStack;
PKCS7 *pkcs7;
PKCS7_SIGNED *pkcs7Signed;
};
struct PKCS7File
{
explicit PKCS7File(const QString &path)
{
if (!isValid()) {
qWarning() << "Unable to prepare X509 certificates structure";
} else {
BIO *input = BIO_new(BIO_s_file());
if (!input) {
qWarning() << "Unable to allocate new BIO for:" << path;
} else {
const QByteArray filename(QFile::encodeName(path));
if (BIO_read_filename(input, const_cast<char *>(filename.constData())) <= 0) {
qWarning() << "Unable to open PKCS7 file:" << path;
} else {
Sep 20, 2019
Sep 20, 2019
429
read_pem_from_bio(input);
430
431
432
433
434
435
436
}
BIO_free(input);
}
}
}
Sep 20, 2019
Sep 20, 2019
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
explicit PKCS7File(const QByteArray &pem)
{
if (!isValid()) {
qWarning() << "Unable to prepare X509 certificates structure";
} else {
BIO *input = BIO_new_mem_buf(pem.constData(), pem.length());
if (!input) {
qWarning() << "Unable to allocate new BIO while importing in-memory PEM";
} else {
read_pem_from_bio(input);
BIO_free(input);
}
}
}
void read_pem_from_bio(BIO *input) {
STACK_OF(X509_INFO) *certificateStack = PEM_X509_INFO_read_bio(input, NULL, NULL, NULL);
if (!certificateStack) {
qWarning() << "Unable to read PKCS7 data";
} else {
while (sk_X509_INFO_num(certificateStack)) {
X509_INFO *certificateInfo = sk_X509_INFO_shift(certificateStack);
if (certificateInfo->x509 != NULL) {
certs.append(certificateInfo->x509);
certificateInfo->x509 = NULL;
}
X509_INFO_free(certificateInfo);
}
sk_X509_INFO_free(certificateStack);
}
}
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
~PKCS7File()
{
}
bool isValid() const
{
return certs.isValid();
}
int count() const
{
return certs.count();
}
const X509List &getCertificates()
{
return certs;
}
private:
X509List certs;
};
class LibCrypto
{
struct Initializer
{
Initializer()
{
// As per: https://wiki.openssl.org/index.php/Library_Initialization#libcrypto_Initialization
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
OPENSSL_config(NULL);
}
~Initializer()
{
FIPS_mode_set(0);
ENGINE_cleanup();
CONF_modules_unload(1);
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
ERR_remove_thread_state(NULL);
ERR_free_strings();
}
};
static Initializer init;
public:
Sep 20, 2019
Sep 20, 2019
520
521
template<class T>
static QList<Certificate> getCertificates(const T &bundleData)
Sep 20, 2019
Sep 20, 2019
523
PKCS7File bundle(bundleData);
Sep 20, 2019
Sep 20, 2019
525
526
527
528
529
530
return bundleToCertificates(bundle);
}
private:
static QList<Certificate> bundleToCertificates(PKCS7File &bundle)
{
QList<Certificate> certificates;
531
532
533
534
535
536
537
538
539
540
541
if (bundle.isValid() && bundle.count() > 0) {
certificates.reserve(bundle.count());
bundle.getCertificates().for_each([&certificates](const X509Certificate &cert) {
certificates.append(Certificate(cert));
});
}
return certificates;
}
};
Sep 20, 2019
Sep 20, 2019
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
LibCrypto::Initializer LibCrypto::init;
const QList<QPair<QString, CertificateModel::BundleType> > &bundlePaths()
{
static QList<QPair<QString, CertificateModel::BundleType> > paths;
if (paths.isEmpty()) {
paths.append(qMakePair(QString("/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem"), CertificateModel::TLSBundle));
paths.append(qMakePair(QString("/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem"), CertificateModel::EmailBundle));
paths.append(qMakePair(QString("/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem"), CertificateModel::ObjectSigningBundle));
}
return paths;
}
CertificateModel::BundleType bundleType(const QString &path)
{
if (path.isEmpty())
return CertificateModel::NoBundle;
const QList<QPair<QString, CertificateModel::BundleType> > &bundles(bundlePaths());
for (auto it = bundles.cbegin(), end = bundles.cend(); it != end; ++it) {
if (it->first == path)
return it->second;
}
return CertificateModel::UserSpecifiedBundle;
}
QString bundlePath(CertificateModel::BundleType type)
{
if (type == CertificateModel::UserSpecifiedBundle)
return QString();
const QList<QPair<QString, CertificateModel::BundleType> > &bundles(bundlePaths());
for (auto it = bundles.cbegin(), end = bundles.cend(); it != end; ++it) {
if (it->second == type)
return it->first;
}
return QStringLiteral("");
}
}
Certificate::Certificate(const X509Certificate &cert)
: m_commonName(cert.subjectElement(NID_commonName))
, m_countryName(cert.subjectElement(NID_countryName))
, m_organizationName(cert.subjectElement(NID_organizationName))
, m_organizationalUnitName(cert.subjectElement(NID_organizationalUnitName))
, m_notValidBefore(cert.notBefore())
, m_notValidAfter(cert.notAfter())
{
// Yield consistent names for the certificates, despite inconsistent naming policy
QString Certificate::*members[] = { &Certificate::m_commonName, &Certificate::m_organizationalUnitName, &Certificate::m_organizationName, &Certificate::m_countryName };
for (auto it = std::begin(members); it != std::end(members); ++it) {
const QString &s(this->*(*it));
if (!s.isEmpty()) {
if (m_primaryName.isEmpty()) {
m_primaryName = s;
} else if (m_secondaryName.isEmpty()) {
m_secondaryName = s;
break;
}
}
}
Sep 20, 2019
Sep 20, 2019
608
609
610
611
612
613
614
615
616
617
618
619
// Matches QSslCertificate::issuerDisplayName() introducd in Qt 5.12
// Returns a name that describes the issuer. It returns the CommonName if
// available, otherwise falls back to the Organization or the first
// OrganizationalUnitName.
m_issuerDisplayName = cert.issuerElement(NID_commonName);
if (m_issuerDisplayName.isEmpty()) {
m_issuerDisplayName = cert.issuerElement(NID_countryName);
}
if (m_issuerDisplayName.isEmpty()) {
m_issuerDisplayName = cert.issuerElement(NID_organizationName);
}
620
621
622
// Populate the details map
m_details.insert(QStringLiteral("Version"), QVariant(cert.version()));
m_details.insert(QStringLiteral("SerialNumber"), QVariant(cert.serialNumber()));
Sep 20, 2019
Sep 20, 2019
623
624
625
m_details.insert(QStringLiteral("SubjectDisplayName"), QVariant(m_primaryName));
m_details.insert(QStringLiteral("OrganizationName"), QVariant(m_organizationName));
m_details.insert(QStringLiteral("IssuerDisplayName"), QVariant(m_issuerDisplayName));
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
QVariantMap validity;
validity.insert(QStringLiteral("NotBefore"), QVariant(cert.notBefore()));
validity.insert(QStringLiteral("NotAfter"), QVariant(cert.notAfter()));
m_details.insert(QStringLiteral("Validity"), QVariant(validity));
QVariantMap issuer;
const QList<QPair<QString, QString>> &issuerDetails(cert.issuerList());
for (auto it = issuerDetails.cbegin(), end = issuerDetails.cend(); it != end; ++it) {
issuer.insert(it->first, QVariant(it->second));
}
m_details.insert(QStringLiteral("Issuer"), QVariant(issuer));
QVariantMap subject;
const QList<QPair<QString, QString>> &subjectDetails(cert.subjectList());
for (auto it = subjectDetails.cbegin(), end = subjectDetails.cend(); it != end; ++it) {
subject.insert(it->first, QVariant(it->second));
}
m_details.insert(QStringLiteral("Subject"), QVariant(subject));
QVariantMap publicKey;
const QList<QPair<QString, QString>> &keyDetails(cert.publicKeyList());
for (auto it = keyDetails.cbegin(), end = keyDetails.cend(); it != end; ++it) {
publicKey.insert(it->first, QVariant(it->second));
}
m_details.insert(QStringLiteral("SubjectPublicKeyInfo"), QVariant(publicKey));
QVariantMap extensions;
const QList<QPair<QString, QString>> &extensionDetails(cert.extensionList());
for (auto it = extensionDetails.cbegin(), end = extensionDetails.cend(); it != end; ++it) {
extensions.insert(it->first, QVariant(it->second));
}
m_details.insert(QStringLiteral("Extensions"), extensions);
QVariantMap signature;
const QList<QPair<QString, QString>> &signatureDetails(cert.signatureList());
for (auto it = signatureDetails.cbegin(), end = signatureDetails.cend(); it != end; ++it) {
signature.insert(it->first, QVariant(it->second));
}
m_details.insert(QStringLiteral("Signature"), signature);
}
CertificateModel::CertificateModel(QObject *parent)
: QAbstractListModel(parent)
, m_type(NoBundle)
{
}
CertificateModel::~CertificateModel()
{
}
CertificateModel::BundleType CertificateModel::bundleType() const
{
return m_type;
}
void CertificateModel::setBundleType(BundleType type)
{
if (m_type != type) {
m_type = type;
const QString path(::bundlePath(m_type));
if (!path.isNull())
setBundlePath(path);
emit bundleTypeChanged();
}
}
QString CertificateModel::bundlePath() const
{
return m_path;
}
void CertificateModel::setBundlePath(const QString &path)
{
if (m_path != path) {
m_path = path;
refresh();
const BundleType type(::bundleType(m_path));
setBundleType(type);
emit bundlePathChanged();
}
}
int CertificateModel::rowCount(const QModelIndex & parent) const
{
Q_UNUSED(parent)
return m_certificates.count();
}
QVariant CertificateModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
if (row < 0 || row >= m_certificates.count()) {
return QVariant();
}
const Certificate &cert = m_certificates.at(row);
switch (role) {
case CommonNameRole:
return cert.commonName();
case CountryNameRole:
return cert.countryName();
case OrganizationNameRole:
return cert.organizationName();
case OrganizationalUnitNameRole:
return cert.organizationalUnitName();
case PrimaryNameRole:
return cert.primaryName();
case SecondaryNameRole:
return cert.secondaryName();
case NotValidBeforeRole:
return cert.notValidBefore();
case NotValidAfterRole:
return cert.notValidAfter();
case DetailsRole:
return cert.details();
default:
break;
}
return QVariant();
}
QHash<int, QByteArray> CertificateModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[CommonNameRole] = "commonName";
roles[CountryNameRole] = "countryName";
roles[OrganizationNameRole] = "organizationName";
roles[OrganizationalUnitNameRole] = "organizationalUnitName";
roles[PrimaryNameRole] = "primaryName";
roles[SecondaryNameRole] = "secondaryName";
roles[NotValidBeforeRole] = "notValidBefore";
roles[NotValidAfterRole] = "notValidAfter";
roles[DetailsRole] = "details";
return roles;
}
void CertificateModel::refresh()
{
beginResetModel();
if (m_path.isEmpty()) {
m_certificates.clear();
} else {
m_certificates = getCertificates(m_path);
std::stable_sort(m_certificates.begin(), m_certificates.end(), [](const Certificate &lhs, const Certificate &rhs) {
int c = lhs.primaryName().compare(rhs.primaryName(), Qt::CaseInsensitive);
if (c < 0)
return true;
if (c > 0)
return false;
c = lhs.secondaryName().compare(rhs.secondaryName(), Qt::CaseInsensitive);
if (c < 0)
return true;
return false;
});
}
endResetModel();
}
QList<Certificate> CertificateModel::getCertificates(const QString &bundlePath)
{
return LibCrypto::getCertificates(bundlePath);
}
Sep 20, 2019
Sep 20, 2019
797
798
799
800
QList<Certificate> CertificateModel::getCertificates(const QByteArray &pem)
{
return LibCrypto::getCertificates(pem);
}