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