|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2010 - 2013 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a widget to show SSL certificate infos. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import QCryptographicHash, QDateTime, qVersion |
|
11 from PyQt4.QtGui import QWidget |
|
12 from PyQt4.QtNetwork import QSslCertificate |
|
13 |
|
14 from .Ui_E5SslInfoWidget import Ui_E5SslInfoWidget |
|
15 |
|
16 import Utilities |
|
17 |
|
18 |
|
19 class E5SslInfoWidget(QWidget, Ui_E5SslInfoWidget): |
|
20 """ |
|
21 Class implementing a widget to show SSL certificate infos. |
|
22 """ |
|
23 def __init__(self, parent=None): |
|
24 """ |
|
25 Constructor |
|
26 |
|
27 @param parent reference to the parent widget (QWidget) |
|
28 """ |
|
29 super().__init__(parent) |
|
30 self.setupUi(self) |
|
31 |
|
32 def showCertificate(self, certificate): |
|
33 """ |
|
34 Public method to show the SSL certificate information. |
|
35 |
|
36 @param certificate reference to the SSL certificate (QSslCertificate) |
|
37 """ |
|
38 self.blacklistedLabel.setVisible(False) |
|
39 self.blacklistedLabel.setStyleSheet( |
|
40 "QLabel { color : white; background-color : red; }") |
|
41 self.expiredLabel.setVisible(False) |
|
42 self.expiredLabel.setStyleSheet( |
|
43 "QLabel { color : white; background-color : red; }") |
|
44 |
|
45 if qVersion() >= "5.0.0": |
|
46 self.subjectCommonNameLabel.setText(self.__certificateString( |
|
47 ", ".join(certificate.subjectInfo(QSslCertificate.CommonName)))) |
|
48 self.subjectOrganizationLabel.setText(self.__certificateString( |
|
49 ", ".join(certificate.subjectInfo(QSslCertificate.Organization)))) |
|
50 self.subjectOrganizationalUnitLabel.setText(self.__certificateString( |
|
51 ", ".join( |
|
52 certificate.subjectInfo(QSslCertificate.OrganizationalUnitName)))) |
|
53 self.issuerCommonNameLabel.setText(self.__certificateString( |
|
54 ", ".join(certificate.issuerInfo(QSslCertificate.CommonName)))) |
|
55 self.issuerOrganizationLabel.setText(self.__certificateString( |
|
56 ", ".join(certificate.issuerInfo(QSslCertificate.Organization)))) |
|
57 self.issuerOrganizationalUnitLabel.setText(self.__certificateString( |
|
58 ", ".join(certificate.issuerInfo(QSslCertificate.OrganizationalUnitName)))) |
|
59 else: |
|
60 self.subjectCommonNameLabel.setText(self.__certificateString( |
|
61 certificate.subjectInfo(QSslCertificate.CommonName))) |
|
62 self.subjectOrganizationLabel.setText(self.__certificateString( |
|
63 certificate.subjectInfo(QSslCertificate.Organization))) |
|
64 self.subjectOrganizationalUnitLabel.setText(self.__certificateString( |
|
65 certificate.subjectInfo(QSslCertificate.OrganizationalUnitName))) |
|
66 self.issuerCommonNameLabel.setText(self.__certificateString( |
|
67 certificate.issuerInfo(QSslCertificate.CommonName))) |
|
68 self.issuerOrganizationLabel.setText(self.__certificateString( |
|
69 certificate.issuerInfo(QSslCertificate.Organization))) |
|
70 self.issuerOrganizationalUnitLabel.setText(self.__certificateString( |
|
71 certificate.issuerInfo(QSslCertificate.OrganizationalUnitName))) |
|
72 self.serialNumberLabel.setText(self.__serialNumber(certificate)) |
|
73 self.effectiveLabel.setText( |
|
74 certificate.effectiveDate().toString("yyyy-MM-dd")) |
|
75 self.expiresLabel.setText( |
|
76 certificate.expiryDate().toString("yyyy-MM-dd")) |
|
77 self.sha1Label.setText(self.__formatHexString( |
|
78 str(certificate.digest(QCryptographicHash.Sha1).toHex(), encoding="ascii"))) |
|
79 self.md5Label.setText(self.__formatHexString( |
|
80 str(certificate.digest(QCryptographicHash.Md5).toHex(), encoding="ascii"))) |
|
81 |
|
82 if (qVersion() >= "5.0.0" and certificate.isBlacklisted()) or \ |
|
83 (qVersion() < "5.0.0" and not certificate.isValid()): |
|
84 # something is wrong; indicate it to the user |
|
85 if self.__hasExpired(certificate.effectiveDate(), certificate.expiryDate()): |
|
86 self.expiredLabel.setVisible(True) |
|
87 else: |
|
88 self.blacklistedLabel.setVisible(True) |
|
89 |
|
90 def __certificateString(self, txt): |
|
91 """ |
|
92 Private method to prepare some text for display. |
|
93 |
|
94 @param txt text to be displayed (string) |
|
95 @return prepared text (string) |
|
96 """ |
|
97 if txt is None or txt == "": |
|
98 return self.trUtf8("<not part of the certificate>") |
|
99 |
|
100 return Utilities.decodeString(txt) |
|
101 |
|
102 def __serialNumber(self, cert): |
|
103 """ |
|
104 Private slot to format the certificate serial number. |
|
105 |
|
106 @param cert reference to the SSL certificate (QSslCertificate) |
|
107 @return formated serial number (string) |
|
108 """ |
|
109 serial = cert.serialNumber() |
|
110 if serial == "": |
|
111 return self.trUtf8("<not part of the certificate>") |
|
112 |
|
113 if ':' in serial: |
|
114 return str(serial, encoding="ascii").upper() |
|
115 else: |
|
116 hexString = hex(int(serial))[2:] |
|
117 return self.__formatHexString(hexString) |
|
118 |
|
119 def __formatHexString(self, hexString): |
|
120 """ |
|
121 Private method to format a hex string for display. |
|
122 |
|
123 @param hexString hex string to be formatted (string) |
|
124 @return formatted string (string) |
|
125 """ |
|
126 hexString = hexString.upper() |
|
127 |
|
128 if len(hexString) % 2 == 1: |
|
129 hexString = '0' + hexString |
|
130 |
|
131 hexList = [] |
|
132 while hexString: |
|
133 hexList.append(hexString[:2]) |
|
134 hexString = hexString[2:] |
|
135 |
|
136 return ':'.join(hexList) |
|
137 |
|
138 def __hasExpired(self, effectiveDate, expiryDate): |
|
139 """ |
|
140 Private method to check for a certificate expiration. |
|
141 |
|
142 @param effectiveDate date the certificate becomes effective (QDateTime) |
|
143 @param expiryDate date the certificate expires (QDateTime) |
|
144 @return flag indicating the expiration status (boolean) |
|
145 """ |
|
146 now = QDateTime.currentDateTime() |
|
147 |
|
148 return now < effectiveDate or now >= expiryDate |