E5Network/E5SslInfoWidget.py

Tue, 07 Jan 2014 09:23:24 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 07 Jan 2014 09:23:24 +0100
branch
5_4_x
changeset 3183
ae1ff1ff7709
parent 3160
209a07d7e401
child 3178
f25fc1364c88
child 3190
a9a94491c4fd
permissions
-rw-r--r--

Prepared release 5.4.0 of eric5.

# -*- coding: utf-8 -*-

# Copyright (c) 2010 - 2014 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a widget to show SSL information.
"""

from PyQt4.QtCore import qVersion, Qt, QUrl, QPoint
from PyQt4.QtGui import QMenu, QGridLayout, QLabel, QSizePolicy
from PyQt4.QtNetwork import QSsl, QSslConfiguration, QSslCertificate

import UI.PixmapCache
import Utilities


class E5SslInfoWidget(QMenu):
    """
    Class implementing a widget to show SSL certificate infos.
    """
    def __init__(self, url, configuration, parent=None):
        """
        Constructor
        
        @param url URL to show SSL info for (QUrl)
        @param configuration SSL configuration (QSslConfiguration)
        @param parent reference to the parent widget (QWidget)
        """
        super().__init__(parent)
        
        self.__url = QUrl(url)
        self.__configuration = QSslConfiguration(configuration)
        
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setMinimumWidth(400)
        
        certList = self.__configuration.peerCertificateChain()
        if certList:
            cert = certList[0]
        else:
            cert = QSslCertificate()
        
        layout = QGridLayout(self)
        rows = 0
        
        ##########################################
        ## Identity Information
        ##########################################
        imageLabel = QLabel(self)
        layout.addWidget(imageLabel, rows, 0, Qt.AlignCenter)
        
        label = QLabel(self)
        label.setWordWrap(True)
        label.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        label.setText(self.trUtf8("Identity"))
        font = label.font()
        font.setBold(True)
        label.setFont(font)
        layout.addWidget(label, rows, 1)
        rows += 1
        
        label = QLabel(self)
        label.setWordWrap(True)
        if cert.isNull():
            label.setText(self.trUtf8(
                "Warning: this site is NOT carrying a certificate."))
            imageLabel.setPixmap(UI.PixmapCache.getPixmap("securityLow32.png"))
        else:
            if qVersion() >= "5.0.0":
                valid = not cert.isBlacklisted()
            else:
                valid = cert.isValid()
            if valid:
                if qVersion() >= "5.0.0":
                    txt = ", ".join(
                        cert.issuerInfo(QSslCertificate.CommonName))
                else:
                    txt = cert.issuerInfo(QSslCertificate.CommonName)
                label.setText(self.trUtf8(
                    "The certificate for this site is valid"
                    " and has been verified by:\n{0}").format(
                    Utilities.decodeString(txt)))
                imageLabel.setPixmap(
                    UI.PixmapCache.getPixmap("securityHigh32.png"))
            else:
                label.setText(self.trUtf8(
                    "The certificate for this site is NOT valid."))
                imageLabel.setPixmap(
                    UI.PixmapCache.getPixmap("securityLow32.png"))
            layout.addWidget(label, rows, 1)
            rows += 1
            
            label = QLabel(self)
            label.setWordWrap(True)
            label.setText(
                '<a href="moresslinfos">' +
                self.trUtf8("Certificate Information") + "</a>")
            label.linkActivated.connect(self.__showCertificateInfos)
            layout.addWidget(label, rows, 1)
            rows += 1
        
        ##########################################
        ## Identity Information
        ##########################################
        imageLabel = QLabel(self)
        layout.addWidget(imageLabel, rows, 0, Qt.AlignCenter)
        
        label = QLabel(self)
        label.setWordWrap(True)
        label.setText(self.trUtf8("Encryption"))
        font = label.font()
        font.setBold(True)
        label.setFont(font)
        layout.addWidget(label, rows, 1)
        rows += 1
        
        cipher = self.__configuration.sessionCipher()
        if cipher.isNull():
            label = QLabel(self)
            label.setWordWrap(True)
            label.setText(self.trUtf8(
                'Your connection to "{0}" is NOT encrypted.\n').format(
                self.__url.host()))
            layout.addWidget(label, rows, 1)
            imageLabel.setPixmap(UI.PixmapCache.getPixmap("securityLow32.png"))
            rows += 1
        else:
            label = QLabel(self)
            label.setWordWrap(True)
            label.setText(self.trUtf8(
                'Your connection to "{0}" is encrypted.').format(
                self.__url.host()))
            layout.addWidget(label, rows, 1)
            
            proto = cipher.protocol()
            if proto == QSsl.SslV3:
                sslVersion = "SSL 3.0"
                imageLabel.setPixmap(
                    UI.PixmapCache.getPixmap("securityHigh32.png"))
            elif proto == QSsl.TlsV1:
                sslVersion = "TLS 1.0"
                imageLabel.setPixmap(
                    UI.PixmapCache.getPixmap("securityHigh32.png"))
            elif proto == QSsl.SslV2:
                sslVersion = "SSL 2.0"
                imageLabel.setPixmap(
                    UI.PixmapCache.getPixmap("securityLow32.png"))
            else:
                sslVersion = self.trUtf8("unknown")
                imageLabel.setPixmap(
                    UI.PixmapCache.getPixmap("securityLow32.png"))
            rows += 1
            
            label = QLabel(self)
            label.setWordWrap(True)
            label.setText(self.trUtf8(
                "It uses protocol: {0}").format(sslVersion))
            layout.addWidget(label, rows, 1)
            rows += 1
            
            label = QLabel(self)
            label.setWordWrap(True)
            label.setText(self.trUtf8(
                "It is encrypted using {0} at {1} bits, "
                "with {2} for message authentication and "
                "{3} as key exchange mechanism.\n\n").format(
                cipher.encryptionMethod(),
                cipher.usedBits(),
                cipher.authenticationMethod(),
                cipher.keyExchangeMethod()))
            layout.addWidget(label, rows, 1)
            rows += 1
    
    def showAt(self, pos):
        """
        Public method to show the widget.
        
        @param pos position to show at (QPoint)
        """
        self.adjustSize()
        xpos = pos.x() - self.width()
        if xpos < 0:
            xpos = 10
        p = QPoint(xpos, pos.y() + 10)
        self.move(p)
        self.show()
    
    def __showCertificateInfos(self):
        """
        Private slot to show certificate information.
        """
        from .E5SslCertificatesInfoDialog import E5SslCertificatesInfoDialog
        dlg = E5SslCertificatesInfoDialog(
            self.__configuration.peerCertificateChain())
        dlg.exec_()
    
    def accept(self):
        """
        Public method to accept the widget.
        """
        self.close()

eric ide

mercurial