E5Network/E5SslCertificatesInfoWidget.py

changeset 2432
b1a2f9054b28
parent 2359
ef81d2d0a031
child 2525
8b507a9a2d40
child 2578
f181c1420148
equal deleted inserted replaced
2430:fd7fa26076d2 2432:b1a2f9054b28
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 pyqtSlot, QCryptographicHash, QDateTime, qVersion
11 from PyQt4.QtGui import QWidget
12 from PyQt4.QtNetwork import QSslCertificate
13
14 from .Ui_E5SslCertificatesInfoWidget import Ui_E5SslCertificatesInfoWidget
15
16 import Utilities
17
18
19 class E5SslCertificatesInfoWidget(QWidget, Ui_E5SslCertificatesInfoWidget):
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 self.__chain = []
33
34 def showCertificateChain(self, certificateChain):
35 """
36 Public method to show the SSL certificates of a certificate chain.
37
38 @param certificateChain list od SSL certificates (list of QSslCertificate)
39 """
40 self.chainLabel.show()
41 self.chainComboBox.show()
42 self.chainComboBox.clear()
43
44 self.__chain = certificateChain[:]
45
46 for cert in self.__chain:
47 name = cert.subjectInfo(QSslCertificate.CommonName)
48 if not name:
49 name = cert.subjectInfo(QSslCertificate.Organization)
50 if not name:
51 name = cert.serialNumber()
52 self.chainComboBox.addItem(name)
53
54 self.on_chainComboBox_activated(0)
55
56 def showCertificate(self, certificate):
57 """
58 Public method to show the SSL certificate information.
59
60 @param certificate reference to the SSL certificate (QSslCertificate)
61 """
62 self.chainLabel.hide()
63 self.chainComboBox.hide()
64 self.chainComboBox.clear()
65
66 self.__chain = []
67
68 self.__showCertificate(certificate)
69
70 def __showCertificate(self, certificate):
71 """
72 Public method to show the SSL certificate information.
73
74 @param certificate reference to the SSL certificate (QSslCertificate)
75 """
76 self.blacklistedLabel.setVisible(False)
77 self.blacklistedLabel.setStyleSheet(
78 "QLabel { color : white; background-color : red; }")
79 self.expiredLabel.setVisible(False)
80 self.expiredLabel.setStyleSheet(
81 "QLabel { color : white; background-color : red; }")
82
83 if qVersion() >= "5.0.0":
84 self.subjectCommonNameLabel.setText(self.__certificateString(
85 ", ".join(certificate.subjectInfo(QSslCertificate.CommonName))))
86 self.subjectOrganizationLabel.setText(self.__certificateString(
87 ", ".join(certificate.subjectInfo(QSslCertificate.Organization))))
88 self.subjectOrganizationalUnitLabel.setText(self.__certificateString(
89 ", ".join(
90 certificate.subjectInfo(QSslCertificate.OrganizationalUnitName))))
91 self.issuerCommonNameLabel.setText(self.__certificateString(
92 ", ".join(certificate.issuerInfo(QSslCertificate.CommonName))))
93 self.issuerOrganizationLabel.setText(self.__certificateString(
94 ", ".join(certificate.issuerInfo(QSslCertificate.Organization))))
95 self.issuerOrganizationalUnitLabel.setText(self.__certificateString(
96 ", ".join(certificate.issuerInfo(QSslCertificate.OrganizationalUnitName))))
97 else:
98 self.subjectCommonNameLabel.setText(self.__certificateString(
99 certificate.subjectInfo(QSslCertificate.CommonName)))
100 self.subjectOrganizationLabel.setText(self.__certificateString(
101 certificate.subjectInfo(QSslCertificate.Organization)))
102 self.subjectOrganizationalUnitLabel.setText(self.__certificateString(
103 certificate.subjectInfo(QSslCertificate.OrganizationalUnitName)))
104 self.issuerCommonNameLabel.setText(self.__certificateString(
105 certificate.issuerInfo(QSslCertificate.CommonName)))
106 self.issuerOrganizationLabel.setText(self.__certificateString(
107 certificate.issuerInfo(QSslCertificate.Organization)))
108 self.issuerOrganizationalUnitLabel.setText(self.__certificateString(
109 certificate.issuerInfo(QSslCertificate.OrganizationalUnitName)))
110 self.serialNumberLabel.setText(self.__serialNumber(certificate))
111 self.effectiveLabel.setText(
112 certificate.effectiveDate().toString("yyyy-MM-dd"))
113 self.expiresLabel.setText(
114 certificate.expiryDate().toString("yyyy-MM-dd"))
115 self.sha1Label.setText(self.__formatHexString(
116 str(certificate.digest(QCryptographicHash.Sha1).toHex(), encoding="ascii")))
117 self.md5Label.setText(self.__formatHexString(
118 str(certificate.digest(QCryptographicHash.Md5).toHex(), encoding="ascii")))
119
120 if (qVersion() >= "5.0.0" and certificate.isBlacklisted()) or \
121 (qVersion() < "5.0.0" and not certificate.isValid()):
122 # something is wrong; indicate it to the user
123 if self.__hasExpired(certificate.effectiveDate(), certificate.expiryDate()):
124 self.expiredLabel.setVisible(True)
125 else:
126 self.blacklistedLabel.setVisible(True)
127
128 def __certificateString(self, txt):
129 """
130 Private method to prepare some text for display.
131
132 @param txt text to be displayed (string)
133 @return prepared text (string)
134 """
135 if txt is None or txt == "":
136 return self.trUtf8("<not part of the certificate>")
137
138 return Utilities.decodeString(txt)
139
140 def __serialNumber(self, cert):
141 """
142 Private slot to format the certificate serial number.
143
144 @param cert reference to the SSL certificate (QSslCertificate)
145 @return formated serial number (string)
146 """
147 serial = cert.serialNumber()
148 if serial == "":
149 return self.trUtf8("<not part of the certificate>")
150
151 if ':' in serial:
152 return str(serial, encoding="ascii").upper()
153 else:
154 hexString = hex(int(serial))[2:]
155 return self.__formatHexString(hexString)
156
157 def __formatHexString(self, hexString):
158 """
159 Private method to format a hex string for display.
160
161 @param hexString hex string to be formatted (string)
162 @return formatted string (string)
163 """
164 hexString = hexString.upper()
165
166 if len(hexString) % 2 == 1:
167 hexString = '0' + hexString
168
169 hexList = []
170 while hexString:
171 hexList.append(hexString[:2])
172 hexString = hexString[2:]
173
174 return ':'.join(hexList)
175
176 def __hasExpired(self, effectiveDate, expiryDate):
177 """
178 Private method to check for a certificate expiration.
179
180 @param effectiveDate date the certificate becomes effective (QDateTime)
181 @param expiryDate date the certificate expires (QDateTime)
182 @return flag indicating the expiration status (boolean)
183 """
184 now = QDateTime.currentDateTime()
185
186 return now < effectiveDate or now >= expiryDate
187
188 @pyqtSlot(int)
189 def on_chainComboBox_activated(self, index):
190 """
191 Private slot to show the certificate info for the selected entry.
192
193 @param index number of the certificate in the certificate chain (integer)
194 """
195 self.__showCertificate(self.__chain[index])

eric ide

mercurial