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