Helpviewer/SslInfoWidget.py

Wed, 02 Jan 2013 10:33:09 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 02 Jan 2013 10:33:09 +0100
branch
5_2_x
changeset 2303
0ed4ed026c16
parent 1509
c0b5e693b0eb
permissions
-rw-r--r--

Updated copyright for 2013.

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

eric ide

mercurial