def ssl_errors(self, reply): try: self.replies.remove(reply) except KeyError: return False ca_cert = reply.sslConfiguration().peerCertificateChain()[-1] if not ca_cert.isValid(): self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + "Das Zertifikat ist nicht gültig." + "</span>" ) return False domain_list = [] for cert in reply.sslConfiguration().peerCertificateChain(): domain_list.append(cert.subjectInfo(cert.SubjectInfo.CommonName)) for key in cert.alternateSubjectNames().keys(): if type(key) == str and key[:3] == "DNS": domain_list.append(cert.alternateSubjectNames()[key]) print(extract_full_domain(self.url_edit.text())) print(domain_list) if extract_full_domain(self.url_edit.text()) not in domain_list: self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + "Das Zertifikat wurde für eine andere Domain ausgestellt." + "</span>" ) return False message_box = QMessageBox() message_box.setText("Ein unbekanntes CA-Zertifikat wurde gefunden.") message_box.setInformativeText( "Das Zertifikat hat den Fingerabdruck " + ":".join(re.findall("(.{2})", str(ca_cert.digest(QCryptographicHash.Sha1).toHex().toUpper()))) + ". Möchten Sie diesem Zertifikat vertrauen?" ) message_box.setStandardButtons(QMessageBox.No | QMessageBox.Yes) message_box.setDefaultButton(QMessageBox.Yes) answer = message_box.exec_() if answer != QMessageBox.Yes: self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + "Sie haben dem Zertifikat nicht vertraut." + "</span>" ) return False if not self.certificate: reply.ignoreSslErrors() self.certificate = ca_cert.toPem() self.save_settings() self.certificate_loaded.emit()
def ssl_errors(self, reply): try: self.replies.remove(reply) except KeyError: return False ca_cert = reply.sslConfiguration().peerCertificateChain()[-1] if not ca_cert.isValid(): self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + 'Das Zertifikat ist nicht gültig.' + '</span>') return False domain_list = [] for cert in reply.sslConfiguration().peerCertificateChain(): domain_list.append(cert.subjectInfo(cert.SubjectInfo.CommonName)) for key in cert.alternateSubjectNames().keys(): if type(key) == str and key[:3] == "DNS": domain_list.append(cert.alternateSubjectNames()[key]) print(extract_full_domain(self.url_edit.text())) print(domain_list) if extract_full_domain(self.url_edit.text()) not in domain_list: self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + 'Das Zertifikat wurde für eine andere Domain ausgestellt.' + '</span>') return False message_box = QMessageBox() message_box.setText("Ein unbekanntes CA-Zertifikat wurde gefunden.") message_box.setInformativeText( "Das Zertifikat hat den Fingerabdruck " + ":".join( re.findall( "(.{2})", str( ca_cert.digest( QCryptographicHash.Sha1).toHex().toUpper()))) + ". Möchten Sie diesem Zertifikat vertrauen?") message_box.setStandardButtons(QMessageBox.No | QMessageBox.Yes) message_box.setDefaultButton(QMessageBox.Yes) answer = message_box.exec_() if answer != QMessageBox.Yes: self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + 'Sie haben dem Zertifikat nicht vertraut.' + '</span>') return False if not self.certificate: reply.ignoreSslErrors() self.certificate = ca_cert.toPem() self.save_settings() self.certificate_loaded.emit()
def url_changed(self): if self.certificate: cert = QSslCertificate(encoded=self.certificate, format=QSsl.Pem) domain_list = [cert.subjectInfo(cert.SubjectInfo.CommonName)] for key in cert.alternateSubjectNames().keys(): if type(key) == str and key[:3] == "DNS": domain_list.append(cert.alternateSubjectNames()[key]) if extract_full_domain(self.url_edit.text()) not in domain_list: self.certificate = "" self.message.setText('<span style="font-size: 10px; color: #aa0000;">' + 'Kein Zertifikat für diese url vorhanden.' + '</span>') else: self.message.setText('<span style="font-size: 10px; color: #aa0000;">' + 'Kein Zertifikat für diese url vorhanden.' + '</span>') self.save_settings()
def url_changed(self): if self.certificate: cert = QSslCertificate(encoded=self.certificate, format=QSsl.Pem) domain_list = [cert.subjectInfo(cert.SubjectInfo.CommonName)] for key in cert.alternateSubjectNames().keys(): if type(key) == str and key[:3] == "DNS": domain_list.append(cert.alternateSubjectNames()[key]) if extract_full_domain(self.url_edit.text()) not in domain_list: self.certificate = "" self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + 'Kein Zertifikat für diese url vorhanden.' + '</span>') else: self.message.setText( '<span style="font-size: 10px; color: #aa0000;">' + 'Kein Zertifikat für diese url vorhanden.' + '</span>') self.save_settings()
def test_extract_full_domain(self): self.assertEqual( "www.test.com", extract_full_domain("http://www.test.com/some/path/index.html")) self.assertEqual( "test.com", extract_full_domain("http://test.com/some/path/index.html")) self.assertEqual( "complicated.subdomain.structure.test.com", extract_full_domain( "http://complicated.subdomain.structure.test.com/some/path/index.html" )) self.assertEqual( "www.test.com", extract_full_domain("https://www.test.com/some/path/index.html")) self.assertEqual( "test.com", extract_full_domain("https://test.com/some/path/index.html")) self.assertEqual( "complicated.subdomain.structure.test.com", extract_full_domain( "https://complicated.subdomain.structure.test.com/some/path/index.html" )) self.assertEqual("test.com", extract_full_domain("test.com")) self.assertEqual("www.test.com", extract_full_domain("www.test.com")) self.assertEqual( "complicated.subdomain.structure.test.com", extract_full_domain("complicated.subdomain.structure.test.com")) self.assertEqual("test.com", extract_full_domain("test.com/path/to/things")) self.assertEqual( "www.amazon.co.jp", extract_full_domain( "www.amazon.co.jp/search=?some(characters)[strange]")) self.assertEqual("english.co.uk", extract_full_domain("english.co.uk")) self.assertEqual("noUrl", extract_full_domain("noUrl"))
def test_extract_full_domain(self): self.assertEqual( "www.test.com", extract_full_domain("http://www.test.com/some/path/index.html")) self.assertEqual( "test.com", extract_full_domain("http://test.com/some/path/index.html")) self.assertEqual( "complicated.subdomain.structure.test.com", extract_full_domain("http://complicated.subdomain.structure.test.com/some/path/index.html")) self.assertEqual( "www.test.com", extract_full_domain("https://www.test.com/some/path/index.html")) self.assertEqual( "test.com", extract_full_domain("https://test.com/some/path/index.html")) self.assertEqual( "complicated.subdomain.structure.test.com", extract_full_domain("https://complicated.subdomain.structure.test.com/some/path/index.html")) self.assertEqual( "test.com", extract_full_domain("test.com")) self.assertEqual( "www.test.com", extract_full_domain("www.test.com")) self.assertEqual( "complicated.subdomain.structure.test.com", extract_full_domain("complicated.subdomain.structure.test.com")) self.assertEqual( "test.com", extract_full_domain("test.com/path/to/things")) self.assertEqual( "www.amazon.co.jp", extract_full_domain("www.amazon.co.jp/search=?some(characters)[strange]")) self.assertEqual( "english.co.uk", extract_full_domain("english.co.uk")) self.assertEqual( "noUrl", extract_full_domain("noUrl"))