Helpviewer/SslInfoDialog.py

Sat, 09 Oct 2010 15:26:36 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 09 Oct 2010 15:26:36 +0200
changeset 664
3061f8bd826e
parent 659
b53e67ae2021
child 753
e19a516f0a97
permissions
-rw-r--r--

Fixed a few issues on Windows systems.

659
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de>
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a dialog to show SSL certificate infos.
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 from PyQt4.QtCore import QCryptographicHash
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 from PyQt4.QtGui import QDialog
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 from PyQt4.QtNetwork import QSslCertificate
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 from .Ui_SslInfoDialog import Ui_SslInfoDialog
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 class SslInfoDialog(QDialog, Ui_SslInfoDialog):
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 Class implementing a dialog to show SSL certificate infos.
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 def __init__(self, certificate, parent = None):
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 Constructor
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 @param certificate reference to the SSL certificate (QSslCertificate)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 @param parent reference to the parent widget (QWidget)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 QDialog.__init__(self, parent)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 self.setupUi(self)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 self.subjectCommonNameLabel.setText(self.__certificateString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 certificate.subjectInfo(QSslCertificate.CommonName)))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 self.subjectOrganizationLabel.setText(self.__certificateString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 certificate.subjectInfo(QSslCertificate.Organization)))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 self.subjectOrganizationalUnitLabel.setText(self.__certificateString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 certificate.subjectInfo(QSslCertificate.OrganizationalUnitName)))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.serialNumberLabel.setText(self.__serialNumber(certificate))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 self.issuerCommonNameLabel.setText(self.__certificateString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 certificate.issuerInfo(QSslCertificate.CommonName)))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.issuerOrganizationLabel.setText(self.__certificateString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 certificate.issuerInfo(QSslCertificate.Organization)))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 self.issuerOrganizationalUnitLabel.setText(self.__certificateString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 certificate.issuerInfo(QSslCertificate.OrganizationalUnitName)))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.effectiveLabel.setText(certificate.effectiveDate().toString("yyyy-MM-dd"))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.expiresLabel.setText(certificate.expiryDate().toString("yyyy-MM-dd"))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.sha1Label.setText(self.__formatHexString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 str(certificate.digest(QCryptographicHash.Sha1).toHex(), encoding = "ascii")))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 self.md5Label.setText(self.__formatHexString(
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 str(certificate.digest(QCryptographicHash.Md5).toHex(), encoding = "ascii")))
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 def __certificateString(self, txt):
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 Private method to prepare some text for display.
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 @param txt text to be displayed (string)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 @return prepared text (string)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 if txt is None or txt == "":
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 return self.trUtf8("<not part of the certificate>")
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 return txt
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 def __serialNumber(self, cert):
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 Private slot to format the certificate serial number.
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 @param cert reference to the SSL certificate (QSslCertificate)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 @return formated serial number (string)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 serial = cert.serialNumber()
664
3061f8bd826e Fixed a few issues on Windows systems.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 659
diff changeset
70 if serial == "":
3061f8bd826e Fixed a few issues on Windows systems.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 659
diff changeset
71 return self.trUtf8("<not part of the certificate>")
3061f8bd826e Fixed a few issues on Windows systems.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 659
diff changeset
72
659
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 if ':' in serial:
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 return str(serial, encoding = "ascii").upper()
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 else:
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 hexString = hex(int(serial))[2:]
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 return self.__formatHexString(hexString)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 def __formatHexString(self, hexString):
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 Private method to format a hex string for display.
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 @param hexString hex string to be formatted (string)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 @return formatted string (string)
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 """
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 hexString = hexString.upper()
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 if len(hexString) % 2 == 1:
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 hexString = '0' + hexString
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 hexList = []
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 while hexString:
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 hexList.append(hexString[:2])
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 hexString = hexString[2:]
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
b53e67ae2021 Added code to show certificate info for https URLs.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 return ':'.join(hexList)

eric ide

mercurial