5 |
5 |
6 """ |
6 """ |
7 Module implementing a SSL error handler. |
7 Module implementing a SSL error handler. |
8 """ |
8 """ |
9 |
9 |
|
10 import contextlib |
|
11 import enum |
10 import platform |
12 import platform |
11 import contextlib |
|
12 |
13 |
13 from PyQt5.QtCore import QObject, QByteArray |
14 from PyQt5.QtCore import QObject, QByteArray |
14 from PyQt5.QtNetwork import ( |
15 from PyQt5.QtNetwork import ( |
15 QSslCertificate, QSslConfiguration, QSslSocket, QSslError, QSsl |
16 QSslCertificate, QSslConfiguration, QSslSocket, QSslError, QSsl |
16 ) |
17 ) |
20 import Preferences |
21 import Preferences |
21 import Utilities |
22 import Utilities |
22 import Globals |
23 import Globals |
23 |
24 |
24 |
25 |
|
26 class E5SslErrorState(enum.Enum): |
|
27 """ |
|
28 Class defining the SSL error handling states. |
|
29 """ |
|
30 NOT_IGNORED = 0 |
|
31 SYSTEM_IGNORED = 1 |
|
32 USER_IGNORED = 2 |
|
33 |
|
34 |
25 class E5SslErrorHandler(QObject): |
35 class E5SslErrorHandler(QObject): |
26 """ |
36 """ |
27 Class implementing a handler for SSL errors. |
37 Class implementing a handler for SSL errors. |
28 |
38 |
29 It also initializes the default SSL configuration with certificates |
39 It also initializes the default SSL configuration with certificates |
30 permanently accepted by the user already. |
40 permanently accepted by the user already. |
31 """ |
41 """ |
32 NotIgnored = 0 |
|
33 SystemIgnored = 1 |
|
34 UserIgnored = 2 |
|
35 |
|
36 def __init__(self, parent=None): |
42 def __init__(self, parent=None): |
37 """ |
43 """ |
38 Constructor |
44 Constructor |
39 |
45 |
40 @param parent reference to the parent object (QObject) |
46 @param parent reference to the parent object (QObject) |
103 |
109 |
104 def sslErrors(self, errors, server, port=-1): |
110 def sslErrors(self, errors, server, port=-1): |
105 """ |
111 """ |
106 Public method to handle SSL errors. |
112 Public method to handle SSL errors. |
107 |
113 |
108 @param errors list of SSL errors (list of QSslError) |
114 @param errors list of SSL errors |
109 @param server name of the server (string) |
115 @type list of QSslError |
110 @param port value of the port (integer) |
116 @param server name of the server |
111 @return tuple indicating to ignore the SSL errors (one of NotIgnored, |
117 @type str |
112 SystemIgnored or UserIgnored) and indicating a change of the |
118 @param port value of the port |
113 default SSL configuration (boolean) |
119 @type int |
|
120 @return tuple indicating to ignore the SSL errors and indicating a |
|
121 change of the default SSL configuration |
|
122 @rtype tuple of (E5SslErrorState, bool) |
114 """ |
123 """ |
115 caMerge = {} |
124 caMerge = {} |
116 certificateDict = Globals.toDict( |
125 certificateDict = Globals.toDict( |
117 Preferences.Prefs.settings.value("Ssl/CaCertificatesDict")) |
126 Preferences.Prefs.settings.value("Ssl/CaCertificatesDict")) |
118 for caServer in certificateDict: |
127 for caServer in certificateDict: |
133 if not err.certificate().isNull(): |
142 if not err.certificate().isNull(): |
134 cert = err.certificate() |
143 cert = err.certificate() |
135 if cert not in caNew: |
144 if cert not in caNew: |
136 caNew.append(cert) |
145 caNew.append(cert) |
137 if not errorStrings: |
146 if not errorStrings: |
138 return E5SslErrorHandler.SystemIgnored, False |
147 return E5SslErrorState.SYSTEM_IGNORED, False |
139 |
148 |
140 errorString = '.</li><li>'.join(errorStrings) |
149 errorString = '.</li><li>'.join(errorStrings) |
141 ret = E5MessageBox.yesNo( |
150 ret = E5MessageBox.yesNo( |
142 None, |
151 None, |
143 self.tr("SSL Errors"), |
152 self.tr("SSL Errors"), |
190 certificateDict[server] = pems |
199 certificateDict[server] = pems |
191 Preferences.Prefs.settings.setValue( |
200 Preferences.Prefs.settings.setValue( |
192 "Ssl/CaCertificatesDict", |
201 "Ssl/CaCertificatesDict", |
193 certificateDict) |
202 certificateDict) |
194 |
203 |
195 return E5SslErrorHandler.UserIgnored, caRet |
204 return E5SslErrorState.USER_IGNORED, caRet |
196 |
205 |
197 else: |
206 else: |
198 return E5SslErrorHandler.NotIgnored, False |
207 return E5SslErrorState.NOT_IGNORED, False |
199 |
208 |
200 def __certToString(self, cert): |
209 def __certToString(self, cert): |
201 """ |
210 """ |
202 Private method to convert a certificate to a formatted string. |
211 Private method to convert a certificate to a formatted string. |
203 |
212 |