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