LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
sslcertificateinfowidget.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
31 #include <QSslCertificate>
32 #include <QDateTime>
33 #include <QVBoxLayout>
34 #include <QDialog>
35 #include <QDialogButtonBox>
36 #include "ui_sslcertificateinfowidget.h"
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
43  : QWidget { parent }
44  , Ui_ { std::make_shared<Ui::SslCertificateInfoWidget> () }
45  {
46  Ui_->setupUi (this);
47  }
48 
49  namespace
50  {
51  QByteArray FormatHash (QByteArray hash)
52  {
53  hash = hash.toUpper ();
54  for (int i = 2; i < hash.size (); i += 3)
55  hash.insert (i, ':');
56  return hash;
57  }
58  }
59 
60  void SslCertificateInfoWidget::SetCertificate (const QSslCertificate& cert)
61  {
62  auto setSubjectInfo = [&cert] (QLabel *label, QSslCertificate::SubjectInfo key)
63  {
64  label->setText (cert.subjectInfo (key).join ("; "));
65  };
66  auto setIssuerInfo = [&cert] (QLabel *label, QSslCertificate::SubjectInfo key)
67  {
68  label->setText (cert.issuerInfo (key).join ("; "));
69  };
70 
71  setSubjectInfo (Ui_->SubjectCommonName_, QSslCertificate::CommonName);
72  setSubjectInfo (Ui_->SubjectOrganization_, QSslCertificate::Organization);
73  setSubjectInfo (Ui_->SubjectUnit_, QSslCertificate::OrganizationalUnitName);
74  setSubjectInfo (Ui_->SubjectCountry_, QSslCertificate::CountryName);
75  setSubjectInfo (Ui_->SubjectState_, QSslCertificate::StateOrProvinceName);
76  setSubjectInfo (Ui_->SubjectCity_, QSslCertificate::LocalityName);
77  setIssuerInfo (Ui_->IssuerCommonName_, QSslCertificate::CommonName);
78  setIssuerInfo (Ui_->IssuerOrganization_, QSslCertificate::Organization);
79  setIssuerInfo (Ui_->IssuerUnit_, QSslCertificate::OrganizationalUnitName);
80  setIssuerInfo (Ui_->IssuerCountry_, QSslCertificate::CountryName);
81  setIssuerInfo (Ui_->IssuerState_, QSslCertificate::StateOrProvinceName);
82  setIssuerInfo (Ui_->IssuerCity_, QSslCertificate::LocalityName);
83 
84  Ui_->SerialNumber_->setText (cert.serialNumber ());
85  Ui_->Md5_->setText (cert.digest (QCryptographicHash::Md5).toHex ());
86  Ui_->Sha1_->setText (FormatHash (cert.digest (QCryptographicHash::Sha1).toHex ()));
87  Ui_->Sha256_->setText (FormatHash (cert.digest (QCryptographicHash::Sha256).toHex ()));
88  Ui_->Sha512_->setText (FormatHash (cert.digest (QCryptographicHash::Sha512).toHex ()));
89 
90  Ui_->StartDate_->setText (QLocale {}.toString (cert.effectiveDate ()));
91  Ui_->EndDate_->setText (QLocale {}.toString (cert.expiryDate ()));
92  }
93 
94  QDialog* MakeCertificateViewerDialog (const QSslCertificate& cert, QWidget *parent)
95  {
96  auto dia = new QDialog { parent };
97  dia->setLayout (new QVBoxLayout);
98 
99  const auto certWidget = new Util::SslCertificateInfoWidget;
100  dia->layout ()->addWidget (certWidget);
101 
102  const auto buttons = new QDialogButtonBox { QDialogButtonBox::Close };
103  QObject::connect (buttons,
104  SIGNAL (accepted ()),
105  dia,
106  SLOT (accept ()));
107  QObject::connect (buttons,
108  SIGNAL (rejected ()),
109  dia,
110  SLOT (reject ()));
111  dia->layout ()->addWidget (buttons);
112 
113  certWidget->SetCertificate (cert);
114  return dia;
115  }
116 }
117 }
QDialog * MakeCertificateViewerDialog(const QSslCertificate &cert, QWidget *parent)