1
1
/*
2
- * Copyright (C ) 2016 Jolla Ltd.
3
- * COntact: Matt Vogt <[email protected] >
2
+ * Copyright (c ) 2016 - 2019 Jolla Ltd.
3
+ * Copyright (c) 2019 Open Mobile Platform LLC.
4
4
*
5
5
* You may use this file under the terms of the BSD license as follows:
6
6
*
@@ -426,28 +426,47 @@ struct PKCS7File
426
426
if (BIO_read_filename (input, const_cast <char *>(filename.constData ())) <= 0 ) {
427
427
qWarning () << " Unable to open PKCS7 file:" << path;
428
428
} else {
429
- STACK_OF (X509_INFO) *certificateStack = PEM_X509_INFO_read_bio (input, NULL , NULL , NULL );
430
- if (!certificateStack) {
431
- qWarning () << " Unable to read PKCS7 file:" << path;
432
- } else {
433
- while (sk_X509_INFO_num (certificateStack)) {
434
- X509_INFO *certificateInfo = sk_X509_INFO_shift (certificateStack);
435
- if (certificateInfo->x509 != NULL ) {
436
- certs.append (certificateInfo->x509 );
437
- certificateInfo->x509 = NULL ;
438
- }
439
- X509_INFO_free (certificateInfo);
440
- }
441
-
442
- sk_X509_INFO_free (certificateStack);
443
- }
429
+ read_pem_from_bio (input);
444
430
}
445
431
446
432
BIO_free (input);
447
433
}
448
434
}
449
435
}
450
436
437
+ explicit PKCS7File (const QByteArray &pem)
438
+ {
439
+ if (!isValid ()) {
440
+ qWarning () << " Unable to prepare X509 certificates structure" ;
441
+ } else {
442
+ BIO *input = BIO_new_mem_buf (pem.constData (), pem.length ());
443
+ if (!input) {
444
+ qWarning () << " Unable to allocate new BIO while importing in-memory PEM" ;
445
+ } else {
446
+ read_pem_from_bio (input);
447
+ BIO_free (input);
448
+ }
449
+ }
450
+ }
451
+
452
+ void read_pem_from_bio (BIO *input) {
453
+ STACK_OF (X509_INFO) *certificateStack = PEM_X509_INFO_read_bio (input, NULL , NULL , NULL );
454
+ if (!certificateStack) {
455
+ qWarning () << " Unable to read PKCS7 data" ;
456
+ } else {
457
+ while (sk_X509_INFO_num (certificateStack)) {
458
+ X509_INFO *certificateInfo = sk_X509_INFO_shift (certificateStack);
459
+ if (certificateInfo->x509 != NULL ) {
460
+ certs.append (certificateInfo->x509 );
461
+ certificateInfo->x509 = NULL ;
462
+ }
463
+ X509_INFO_free (certificateInfo);
464
+ }
465
+
466
+ sk_X509_INFO_free (certificateStack);
467
+ }
468
+ }
469
+
451
470
~PKCS7File ()
452
471
{
453
472
}
@@ -498,11 +517,17 @@ class LibCrypto
498
517
static Initializer init;
499
518
500
519
public:
501
- static QList<Certificate> getCertificates (const QString &bundlePath)
520
+ template <class T >
521
+ static QList<Certificate> getCertificates (const T &bundleData)
502
522
{
503
- QList<Certificate> certificates ;
523
+ PKCS7File bundle (bundleData) ;
504
524
505
- PKCS7File bundle (bundlePath);
525
+ return bundleToCertificates (bundle);
526
+ }
527
+ private:
528
+ static QList<Certificate> bundleToCertificates (PKCS7File &bundle)
529
+ {
530
+ QList<Certificate> certificates;
506
531
if (bundle.isValid () && bundle.count () > 0 ) {
507
532
certificates.reserve (bundle.count ());
508
533
bundle.getCertificates ().for_each ([&certificates](const X509Certificate &cert) {
@@ -514,6 +539,7 @@ class LibCrypto
514
539
}
515
540
};
516
541
542
+
517
543
LibCrypto::Initializer LibCrypto::init;
518
544
519
545
const QList<QPair<QString, CertificateModel::BundleType> > &bundlePaths ()
@@ -579,9 +605,24 @@ Certificate::Certificate(const X509Certificate &cert)
579
605
}
580
606
}
581
607
608
+ // Matches QSslCertificate::issuerDisplayName() introducd in Qt 5.12
609
+ // Returns a name that describes the issuer. It returns the CommonName if
610
+ // available, otherwise falls back to the Organization or the first
611
+ // OrganizationalUnitName.
612
+ m_issuerDisplayName = cert.issuerElement (NID_commonName);
613
+ if (m_issuerDisplayName.isEmpty ()) {
614
+ m_issuerDisplayName = cert.issuerElement (NID_countryName);
615
+ }
616
+ if (m_issuerDisplayName.isEmpty ()) {
617
+ m_issuerDisplayName = cert.issuerElement (NID_organizationName);
618
+ }
619
+
582
620
// Populate the details map
583
621
m_details.insert (QStringLiteral (" Version" ), QVariant (cert.version ()));
584
622
m_details.insert (QStringLiteral (" SerialNumber" ), QVariant (cert.serialNumber ()));
623
+ m_details.insert (QStringLiteral (" SubjectDisplayName" ), QVariant (m_primaryName));
624
+ m_details.insert (QStringLiteral (" OrganizationName" ), QVariant (m_organizationName));
625
+ m_details.insert (QStringLiteral (" IssuerDisplayName" ), QVariant (m_issuerDisplayName));
585
626
586
627
QVariantMap validity;
587
628
validity.insert (QStringLiteral (" NotBefore" ), QVariant (cert.notBefore ()));
@@ -753,3 +794,7 @@ QList<Certificate> CertificateModel::getCertificates(const QString &bundlePath)
753
794
return LibCrypto::getCertificates (bundlePath);
754
795
}
755
796
797
+ QList<Certificate> CertificateModel::getCertificates (const QByteArray &pem)
798
+ {
799
+ return LibCrypto::getCertificates (pem);
800
+ }
0 commit comments