14 from PyQt6.QtWidgets import QDialog, QTreeWidgetItem |
14 from PyQt6.QtWidgets import QDialog, QTreeWidgetItem |
15 |
15 |
16 with contextlib.suppress(ImportError): |
16 with contextlib.suppress(ImportError): |
17 from PyQt6.QtNetwork import QSslCertificate, QSslConfiguration, QSsl |
17 from PyQt6.QtNetwork import QSslCertificate, QSslConfiguration, QSsl |
18 |
18 |
19 from eric7 import EricUtilities, Preferences, Utilities |
19 from eric7 import EricUtilities |
20 from eric7.EricGui import EricPixmapCache |
20 from eric7.EricGui import EricPixmapCache |
21 from eric7.EricWidgets import EricFileDialog, EricMessageBox |
21 from eric7.EricWidgets import EricFileDialog, EricMessageBox |
22 |
22 |
23 from .Ui_EricSslCertificatesDialog import Ui_EricSslCertificatesDialog |
23 from .Ui_EricSslCertificatesDialog import Ui_EricSslCertificatesDialog |
24 |
24 |
28 Class implementing a dialog to show and edit all certificates. |
28 Class implementing a dialog to show and edit all certificates. |
29 """ |
29 """ |
30 |
30 |
31 CertRole = Qt.ItemDataRole.UserRole + 1 |
31 CertRole = Qt.ItemDataRole.UserRole + 1 |
32 |
32 |
33 def __init__(self, parent=None): |
33 def __init__(self, settings, parent=None): |
34 """ |
34 """ |
35 Constructor |
35 Constructor |
36 |
36 |
|
37 @param settings reference to the settings object |
|
38 @type QSettings |
37 @param parent reference to the parent widget |
39 @param parent reference to the parent widget |
38 @type QWidget |
40 @type QWidget |
39 """ |
41 """ |
40 super().__init__(parent) |
42 super().__init__(parent) |
41 self.setupUi(self) |
43 self.setupUi(self) |
|
44 |
|
45 self.__settings = settings |
42 |
46 |
43 self.serversViewButton.setIcon(EricPixmapCache.getIcon("certificates")) |
47 self.serversViewButton.setIcon(EricPixmapCache.getIcon("certificates")) |
44 self.serversDeleteButton.setIcon(EricPixmapCache.getIcon("certificateDelete")) |
48 self.serversDeleteButton.setIcon(EricPixmapCache.getIcon("certificateDelete")) |
45 self.serversExportButton.setIcon(EricPixmapCache.getIcon("certificateExport")) |
49 self.serversExportButton.setIcon(EricPixmapCache.getIcon("certificateExport")) |
46 self.serversImportButton.setIcon(EricPixmapCache.getIcon("certificateImport")) |
50 self.serversImportButton.setIcon(EricPixmapCache.getIcon("certificateImport")) |
56 def __populateServerCertificatesTree(self): |
60 def __populateServerCertificatesTree(self): |
57 """ |
61 """ |
58 Private slot to populate the server certificates tree. |
62 Private slot to populate the server certificates tree. |
59 """ |
63 """ |
60 certificateDict = EricUtilities.toDict( |
64 certificateDict = EricUtilities.toDict( |
61 Preferences.getSettings().value("Ssl/CaCertificatesDict") |
65 self.__settings.value("Ssl/CaCertificatesDict") |
62 ) |
66 ) |
63 for server in certificateDict: |
67 for server in certificateDict: |
64 for cert in QSslCertificate.fromData(certificateDict[server]): |
68 for cert in QSslCertificate.fromData(certificateDict[server]): |
65 self.__createServerCertificateEntry(server, cert) |
69 self.__createServerCertificateEntry(server, cert) |
66 |
70 |
76 @type str |
80 @type str |
77 @param cert certificate to insert |
81 @param cert certificate to insert |
78 @type QSslCertificate |
82 @type QSslCertificate |
79 """ |
83 """ |
80 # step 1: extract the info to be shown |
84 # step 1: extract the info to be shown |
81 organisation = Utilities.decodeString( |
85 organisation = EricUtilities.decodeString( |
82 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.Organization)) |
86 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.Organization)) |
83 ) |
87 ) |
84 commonName = Utilities.decodeString( |
88 commonName = EricUtilities.decodeString( |
85 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.CommonName)) |
89 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.CommonName)) |
86 ) |
90 ) |
87 if organisation is None or organisation == "": |
91 if organisation is None or organisation == "": |
88 organisation = self.tr("(Unknown)") |
92 organisation = self.tr("(Unknown)") |
89 if commonName is None or commonName == "": |
93 if commonName is None or commonName == "": |
167 self.serversCertificatesTree.indexOfTopLevelItem(parent) |
171 self.serversCertificatesTree.indexOfTopLevelItem(parent) |
168 ) |
172 ) |
169 |
173 |
170 # delete the certificate from the user certificate store |
174 # delete the certificate from the user certificate store |
171 certificateDict = EricUtilities.toDict( |
175 certificateDict = EricUtilities.toDict( |
172 Preferences.getSettings().value("Ssl/CaCertificatesDict") |
176 self.__settings.value("Ssl/CaCertificatesDict") |
173 ) |
177 ) |
174 if server in certificateDict: |
178 if server in certificateDict: |
175 certs = [ |
179 certs = [ |
176 c.toPem() for c in QSslCertificate.fromData(certificateDict[server]) |
180 c.toPem() for c in QSslCertificate.fromData(certificateDict[server]) |
177 ] |
181 ] |
182 for cert in certs: |
186 for cert in certs: |
183 pems.append(cert + b"\n") |
187 pems.append(cert + b"\n") |
184 certificateDict[server] = pems |
188 certificateDict[server] = pems |
185 else: |
189 else: |
186 del certificateDict[server] |
190 del certificateDict[server] |
187 Preferences.getSettings().setValue( |
191 self.__settings.setValue("Ssl/CaCertificatesDict", certificateDict) |
188 "Ssl/CaCertificatesDict", certificateDict |
|
189 ) |
|
190 |
192 |
191 # delete the certificate from the default certificates |
193 # delete the certificate from the default certificates |
192 self.__updateDefaultConfiguration() |
194 self.__updateDefaultConfiguration() |
193 |
195 |
194 @pyqtSlot() |
196 @pyqtSlot() |
198 """ |
200 """ |
199 certs = self.__importCertificate() |
201 certs = self.__importCertificate() |
200 if certs: |
202 if certs: |
201 server = "*" |
203 server = "*" |
202 certificateDict = EricUtilities.toDict( |
204 certificateDict = EricUtilities.toDict( |
203 Preferences.getSettings().value("Ssl/CaCertificatesDict") |
205 self.__settings.value("Ssl/CaCertificatesDict") |
204 ) |
206 ) |
205 if server in certificateDict: |
207 if server in certificateDict: |
206 sCerts = QSslCertificate.fromData(certificateDict[server]) |
208 sCerts = QSslCertificate.fromData(certificateDict[server]) |
207 else: |
209 else: |
208 sCerts = [] |
210 sCerts = [] |
217 self, |
219 self, |
218 self.tr("Import Certificate"), |
220 self.tr("Import Certificate"), |
219 self.tr( |
221 self.tr( |
220 """<p>The certificate <b>{0}</b> already exists.""" |
222 """<p>The certificate <b>{0}</b> already exists.""" |
221 """ Skipping.</p>""" |
223 """ Skipping.</p>""" |
222 ).format(Utilities.decodeString(commonStr)), |
224 ).format(EricUtilities.decodeString(commonStr)), |
223 ) |
225 ) |
224 else: |
226 else: |
225 pems.append(cert.toPem() + b"\n") |
227 pems.append(cert.toPem() + b"\n") |
226 if server not in certificateDict: |
228 if server not in certificateDict: |
227 certificateDict[server] = QByteArray() |
229 certificateDict[server] = QByteArray() |
228 certificateDict[server].append(pems) |
230 certificateDict[server].append(pems) |
229 Preferences.getSettings().setValue( |
231 self.__settings.setValue("Ssl/CaCertificatesDict", certificateDict) |
230 "Ssl/CaCertificatesDict", certificateDict |
|
231 ) |
|
232 |
232 |
233 self.serversCertificatesTree.clear() |
233 self.serversCertificatesTree.clear() |
234 self.__populateServerCertificatesTree() |
234 self.__populateServerCertificatesTree() |
235 |
235 |
236 self.__updateDefaultConfiguration() |
236 self.__updateDefaultConfiguration() |
253 """ |
253 """ |
254 Private method to update the default SSL configuration. |
254 Private method to update the default SSL configuration. |
255 """ |
255 """ |
256 caList = self.__getSystemCaCertificates() |
256 caList = self.__getSystemCaCertificates() |
257 certificateDict = EricUtilities.toDict( |
257 certificateDict = EricUtilities.toDict( |
258 Preferences.getSettings().value("Ssl/CaCertificatesDict") |
258 self.__settings.value("Ssl/CaCertificatesDict") |
259 ) |
259 ) |
260 for server in certificateDict: |
260 for server in certificateDict: |
261 for cert in QSslCertificate.fromData(certificateDict[server]): |
261 for cert in QSslCertificate.fromData(certificateDict[server]): |
262 if cert not in caList: |
262 if cert not in caList: |
263 caList.append(cert) |
263 caList.append(cert) |
271 |
271 |
272 @return list of system certificates |
272 @return list of system certificates |
273 @rtype list of QSslCertificate |
273 @rtype list of QSslCertificate |
274 """ |
274 """ |
275 caList = QSslCertificate.fromData( |
275 caList = QSslCertificate.fromData( |
276 EricUtilities.toByteArray( |
276 EricUtilities.toByteArray(self.__settings.value("Help/SystemCertificates")) |
277 Preferences.getSettings().value("Help/SystemCertificates") |
|
278 ) |
|
279 ) |
277 ) |
280 if not caList: |
278 if not caList: |
281 caList = QSslConfiguration.systemCaCertificates() |
279 caList = QSslConfiguration.systemCaCertificates() |
282 return caList |
280 return caList |
283 |
281 |
299 |
297 |
300 @param cert certificate to insert |
298 @param cert certificate to insert |
301 @type QSslCertificate |
299 @type QSslCertificate |
302 """ |
300 """ |
303 # step 1: extract the info to be shown |
301 # step 1: extract the info to be shown |
304 organisation = Utilities.decodeString( |
302 organisation = EricUtilities.decodeString( |
305 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.Organization)) |
303 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.Organization)) |
306 ) |
304 ) |
307 commonName = Utilities.decodeString( |
305 commonName = EricUtilities.decodeString( |
308 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.CommonName)) |
306 ", ".join(cert.subjectInfo(QSslCertificate.SubjectInfo.CommonName)) |
309 ) |
307 ) |
310 if organisation is None or organisation == "": |
308 if organisation is None or organisation == "": |
311 organisation = self.tr("(Unknown)") |
309 organisation = self.tr("(Unknown)") |
312 if commonName is None or commonName == "": |
310 if commonName is None or commonName == "": |
416 self, |
414 self, |
417 self.tr("Import Certificate"), |
415 self.tr("Import Certificate"), |
418 self.tr( |
416 self.tr( |
419 """<p>The certificate <b>{0}</b> already exists.""" |
417 """<p>The certificate <b>{0}</b> already exists.""" |
420 """ Skipping.</p>""" |
418 """ Skipping.</p>""" |
421 ).format(Utilities.decodeString(commonStr)), |
419 ).format(EricUtilities.decodeString(commonStr)), |
422 ) |
420 ) |
423 else: |
421 else: |
424 caCerts.append(cert) |
422 caCerts.append(cert) |
425 |
423 |
426 pems = QByteArray() |
424 pems = QByteArray() |
427 for cert in caCerts: |
425 for cert in caCerts: |
428 pems.append(cert.toPem() + b"\n") |
426 pems.append(cert.toPem() + b"\n") |
429 Preferences.getSettings().setValue("Help/SystemCertificates", pems) |
427 self.__settings.setValue("Help/SystemCertificates", pems) |
430 |
428 |
431 self.caCertificatesTree.clear() |
429 self.caCertificatesTree.clear() |
432 self.__populateCaCertificatesTree() |
430 self.__populateCaCertificatesTree() |
433 |
431 |
434 self.__updateDefaultConfiguration() |
432 self.__updateDefaultConfiguration() |