Helpviewer/SslInfoDialog.py

changeset 1427
09d6731b73ad
parent 1367
a652a6794953
child 1509
c0b5e693b0eb
equal deleted inserted replaced
1425:dedba69cb847 1427:09d6731b73ad
5 5
6 """ 6 """
7 Module implementing a dialog to show SSL certificate infos. 7 Module implementing a dialog to show SSL certificate infos.
8 """ 8 """
9 9
10 from PyQt4.QtCore import QCryptographicHash, QDateTime, Qt
11 from PyQt4.QtGui import QDialog 10 from PyQt4.QtGui import QDialog
12 from PyQt4.QtNetwork import QSslCertificate
13 11
14 from .Ui_SslInfoDialog import Ui_SslInfoDialog 12 from .Ui_SslInfoDialog import Ui_SslInfoDialog
15
16 import Utilities
17 13
18 14
19 class SslInfoDialog(QDialog, Ui_SslInfoDialog): 15 class SslInfoDialog(QDialog, Ui_SslInfoDialog):
20 """ 16 """
21 Class implementing a dialog to show SSL certificate infos. 17 Class implementing a dialog to show SSL certificate infos.
28 @param parent reference to the parent widget (QWidget) 24 @param parent reference to the parent widget (QWidget)
29 """ 25 """
30 super().__init__(parent) 26 super().__init__(parent)
31 self.setupUi(self) 27 self.setupUi(self)
32 28
33 self.blacklistedLabel.setVisible(False) 29 self.sslWidget.showCertificate(certificate)
34 self.blacklistedLabel.setStyleSheet(
35 "QLabel { color : white; background-color : red; }")
36 self.expiredLabel.setVisible(False)
37 self.expiredLabel.setStyleSheet(
38 "QLabel { color : white; background-color : red; }")
39
40 self.subjectCommonNameLabel.setText(self.__certificateString(
41 certificate.subjectInfo(QSslCertificate.CommonName)))
42 self.subjectOrganizationLabel.setText(self.__certificateString(
43 certificate.subjectInfo(QSslCertificate.Organization)))
44 self.subjectOrganizationalUnitLabel.setText(self.__certificateString(
45 certificate.subjectInfo(QSslCertificate.OrganizationalUnitName)))
46 self.serialNumberLabel.setText(self.__serialNumber(certificate))
47 self.issuerCommonNameLabel.setText(self.__certificateString(
48 certificate.issuerInfo(QSslCertificate.CommonName)))
49 self.issuerOrganizationLabel.setText(self.__certificateString(
50 certificate.issuerInfo(QSslCertificate.Organization)))
51 self.issuerOrganizationalUnitLabel.setText(self.__certificateString(
52 certificate.issuerInfo(QSslCertificate.OrganizationalUnitName)))
53 self.effectiveLabel.setText(Qt.escape(
54 certificate.effectiveDate().toString("yyyy-MM-dd")))
55 self.expiresLabel.setText(Qt.escape(
56 certificate.expiryDate().toString("yyyy-MM-dd")))
57 self.sha1Label.setText(self.__formatHexString(
58 str(certificate.digest(QCryptographicHash.Sha1).toHex(), encoding="ascii")))
59 self.md5Label.setText(self.__formatHexString(
60 str(certificate.digest(QCryptographicHash.Md5).toHex(), encoding="ascii")))
61
62 if not certificate.isValid():
63 # something is wrong; indicate it to the user
64 if self.__hasExpired(certificate.effectiveDate(), certificate.expiryDate()):
65 self.expiredLabel.setVisible(True)
66 else:
67 self.blacklistedLabel.setVisible(True)
68
69 def __certificateString(self, txt):
70 """
71 Private method to prepare some text for display.
72
73 @param txt text to be displayed (string)
74 @return prepared text (string)
75 """
76 if txt is None or txt == "":
77 return self.trUtf8("<not part of the certificate>")
78
79 return Qt.escape(Utilities.decodeString(txt))
80
81 def __serialNumber(self, cert):
82 """
83 Private slot to format the certificate serial number.
84
85 @param cert reference to the SSL certificate (QSslCertificate)
86 @return formated serial number (string)
87 """
88 serial = cert.serialNumber()
89 if serial == "":
90 return self.trUtf8("<not part of the certificate>")
91
92 if ':' in serial:
93 return str(serial, encoding="ascii").upper()
94 else:
95 hexString = hex(int(serial))[2:]
96 return self.__formatHexString(hexString)
97
98 def __formatHexString(self, hexString):
99 """
100 Private method to format a hex string for display.
101
102 @param hexString hex string to be formatted (string)
103 @return formatted string (string)
104 """
105 hexString = hexString.upper()
106
107 if len(hexString) % 2 == 1:
108 hexString = '0' + hexString
109
110 hexList = []
111 while hexString:
112 hexList.append(hexString[:2])
113 hexString = hexString[2:]
114
115 return Qt.escape(':'.join(hexList))
116
117 def __hasExpired(self, effectiveDate, expiryDate):
118 """
119 Private method to check for a certificate expiration.
120
121 @param effectiveDate date the certificate becomes effective (QDateTime)
122 @param expiryDate date the certificate expires (QDateTime)
123 @return flag indicating the expiration status (boolean)
124 """
125 now = QDateTime.currentDateTime()
126
127 return now < effectiveDate or now >= expiryDate

eric ide

mercurial