src/eric7/EricNetwork/EricSslCertificatesInfoWidget.py

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

eric ide

mercurial