eric6/E5Network/E5SslCertificatesInfoWidget.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7192
a22eee00b052
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a widget to show SSL certificate infos.
8 """
9
10 from __future__ import unicode_literals
11 try:
12 str = unicode
13 except NameError:
14 pass
15
16 from PyQt5.QtCore import pyqtSlot, QCryptographicHash, QDateTime
17 from PyQt5.QtWidgets import QWidget
18 from PyQt5.QtNetwork import QSslCertificate
19
20 from .Ui_E5SslCertificatesInfoWidget import Ui_E5SslCertificatesInfoWidget
21
22 import Utilities
23 from Globals import qVersionTuple
24
25
26 class E5SslCertificatesInfoWidget(QWidget, Ui_E5SslCertificatesInfoWidget):
27 """
28 Class implementing a widget to show SSL certificate infos.
29 """
30 def __init__(self, parent=None):
31 """
32 Constructor
33
34 @param parent reference to the parent widget (QWidget)
35 """
36 super(E5SslCertificatesInfoWidget, self).__init__(parent)
37 self.setupUi(self)
38
39 self.__chain = []
40
41 def showCertificateChain(self, certificateChain):
42 """
43 Public method to show the SSL certificates of a certificate chain.
44
45 @param certificateChain list od SSL certificates
46 (list of QSslCertificate)
47 """
48 self.chainLabel.show()
49 self.chainComboBox.show()
50 self.chainComboBox.clear()
51
52 self.__chain = certificateChain[:]
53
54 for cert in self.__chain:
55 if qVersionTuple() >= (5, 0, 0):
56 name = ", ".join(cert.subjectInfo(QSslCertificate.CommonName))
57 else:
58 name = cert.subjectInfo(QSslCertificate.CommonName)
59 if not name:
60 if qVersionTuple() >= (5, 0, 0):
61 name = ", ".join(
62 cert.subjectInfo(QSslCertificate.Organization))
63 else:
64 name = cert.subjectInfo(QSslCertificate.Organization)
65 if not name:
66 name = cert.serialNumber()
67 self.chainComboBox.addItem(name)
68
69 self.on_chainComboBox_activated(0)
70
71 def showCertificate(self, certificate):
72 """
73 Public method to show the SSL certificate information.
74
75 @param certificate reference to the SSL certificate (QSslCertificate)
76 """
77 self.chainLabel.hide()
78 self.chainComboBox.hide()
79 self.chainComboBox.clear()
80
81 self.__chain = []
82
83 self.__showCertificate(certificate)
84
85 def __showCertificate(self, certificate):
86 """
87 Private method to show the SSL certificate information.
88
89 @param certificate reference to the SSL certificate (QSslCertificate)
90 """
91 self.blacklistedLabel.setVisible(False)
92 self.blacklistedLabel.setStyleSheet(
93 "QLabel { color : white; background-color : red; }")
94 self.expiredLabel.setVisible(False)
95 self.expiredLabel.setStyleSheet(
96 "QLabel { color : white; background-color : red; }")
97
98 if qVersionTuple() >= (5, 0, 0):
99 self.subjectCommonNameLabel.setText(self.__certificateString(
100 ", ".join(certificate.subjectInfo(
101 QSslCertificate.CommonName))))
102 self.subjectOrganizationLabel.setText(self.__certificateString(
103 ", ".join(certificate.subjectInfo(
104 QSslCertificate.Organization))))
105 self.subjectOrganizationalUnitLabel.setText(
106 self.__certificateString(", ".join(
107 certificate.subjectInfo(
108 QSslCertificate.OrganizationalUnitName))))
109 self.issuerCommonNameLabel.setText(self.__certificateString(
110 ", ".join(certificate.issuerInfo(QSslCertificate.CommonName))))
111 self.issuerOrganizationLabel.setText(self.__certificateString(
112 ", ".join(certificate.issuerInfo(
113 QSslCertificate.Organization))))
114 self.issuerOrganizationalUnitLabel.setText(
115 self.__certificateString(", ".join(
116 certificate.issuerInfo(
117 QSslCertificate.OrganizationalUnitName))))
118 else:
119 self.subjectCommonNameLabel.setText(self.__certificateString(
120 certificate.subjectInfo(QSslCertificate.CommonName)))
121 self.subjectOrganizationLabel.setText(self.__certificateString(
122 certificate.subjectInfo(QSslCertificate.Organization)))
123 self.subjectOrganizationalUnitLabel.setText(
124 self.__certificateString(certificate.subjectInfo(
125 QSslCertificate.OrganizationalUnitName)))
126 self.issuerCommonNameLabel.setText(self.__certificateString(
127 certificate.issuerInfo(QSslCertificate.CommonName)))
128 self.issuerOrganizationLabel.setText(self.__certificateString(
129 certificate.issuerInfo(QSslCertificate.Organization)))
130 self.issuerOrganizationalUnitLabel.setText(
131 self.__certificateString(certificate.issuerInfo(
132 QSslCertificate.OrganizationalUnitName)))
133 self.serialNumberLabel.setText(self.__serialNumber(certificate))
134 self.effectiveLabel.setText(
135 certificate.effectiveDate().toString("yyyy-MM-dd"))
136 self.expiresLabel.setText(
137 certificate.expiryDate().toString("yyyy-MM-dd"))
138 self.sha1Label.setText(self.__formatHexString(
139 str(certificate.digest(QCryptographicHash.Sha1).toHex(),
140 encoding="ascii")))
141 self.md5Label.setText(self.__formatHexString(
142 str(certificate.digest(QCryptographicHash.Md5).toHex(),
143 encoding="ascii")))
144
145 if (qVersionTuple() >= (5, 0, 0) and certificate.isBlacklisted()) or \
146 (qVersionTuple() < (5, 0, 0) and not certificate.isValid()):
147 # something is wrong; indicate it to the user
148 if self.__hasExpired(certificate.effectiveDate(),
149 certificate.expiryDate()):
150 self.expiredLabel.setVisible(True)
151 else:
152 self.blacklistedLabel.setVisible(True)
153
154 def __certificateString(self, txt):
155 """
156 Private method to prepare some text for display.
157
158 @param txt text to be displayed (string)
159 @return prepared text (string)
160 """
161 if txt is None or txt == "":
162 return self.tr("<not part of the certificate>")
163
164 return Utilities.decodeString(txt)
165
166 def __serialNumber(self, cert):
167 """
168 Private slot to format the certificate serial number.
169
170 @param cert reference to the SSL certificate (QSslCertificate)
171 @return formated serial number (string)
172 """
173 serial = cert.serialNumber()
174 if serial == "":
175 return self.tr("<not part of the certificate>")
176
177 if b':' in serial:
178 return str(serial, encoding="ascii").upper()
179 else:
180 hexString = hex(int(serial))[2:]
181 return self.__formatHexString(hexString)
182
183 def __formatHexString(self, hexString):
184 """
185 Private method to format a hex string for display.
186
187 @param hexString hex string to be formatted (string)
188 @return formatted string (string)
189 """
190 hexString = hexString.upper()
191
192 if len(hexString) % 2 == 1:
193 hexString = '0' + hexString
194
195 hexList = []
196 while hexString:
197 hexList.append(hexString[:2])
198 hexString = hexString[2:]
199
200 return ':'.join(hexList)
201
202 def __hasExpired(self, effectiveDate, expiryDate):
203 """
204 Private method to check for a certificate expiration.
205
206 @param effectiveDate date the certificate becomes effective (QDateTime)
207 @param expiryDate date the certificate expires (QDateTime)
208 @return flag indicating the expiration status (boolean)
209 """
210 now = QDateTime.currentDateTime()
211
212 return now < effectiveDate or now >= expiryDate
213
214 @pyqtSlot(int)
215 def on_chainComboBox_activated(self, index):
216 """
217 Private slot to show the certificate info for the selected entry.
218
219 @param index number of the certificate in the certificate chain
220 (integer)
221 """
222 self.__showCertificate(self.__chain[index])

eric ide

mercurial