| ... |
... |
@@ -12,8 +12,11 @@ |
|
12
|
12
|
# You should have received a copy of the GNU General Public License
|
|
13
|
13
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
14
|
14
|
|
|
|
15
|
+from __future__ import annotations
|
|
15
|
16
|
|
|
16
|
17
|
import logging
|
|
|
18
|
+from pathlib import Path
|
|
|
19
|
+from typing import Optional
|
|
17
|
20
|
|
|
18
|
21
|
from gi.repository import GLib
|
|
19
|
22
|
from gi.repository import Gio
|
| ... |
... |
@@ -30,18 +33,18 @@ |
|
30
|
33
|
class CertificateStore:
|
|
31
|
34
|
def __init__(self):
|
|
32
|
35
|
self._path = configpaths.get('CERT_STORE')
|
|
33
|
|
- self._certs = []
|
|
|
36
|
+ self._certs: list[Gio.TlsCertificate] = []
|
|
34
|
37
|
|
|
35
|
38
|
self._load_certificates()
|
|
36
|
39
|
|
|
37
|
|
- def _get_random_path(self):
|
|
|
40
|
+ def _get_random_path(self) -> Path:
|
|
38
|
41
|
filename = get_random_string()
|
|
39
|
42
|
path = self._path / filename
|
|
40
|
43
|
if path.exists():
|
|
41
|
44
|
return self._get_random_path()
|
|
42
|
45
|
return path
|
|
43
|
46
|
|
|
44
|
|
- def _load_certificates(self):
|
|
|
47
|
+ def _load_certificates(self) -> None:
|
|
45
|
48
|
for path in self._path.iterdir():
|
|
46
|
49
|
if path.is_dir():
|
|
47
|
50
|
continue
|
| ... |
... |
@@ -59,7 +62,7 @@ def _load_certificates(self): |
|
59
|
62
|
def get_certificates(self):
|
|
60
|
63
|
return list(self._certs)
|
|
61
|
64
|
|
|
62
|
|
- def add_certificate(self, certificate):
|
|
|
65
|
+ def add_certificate(self, certificate: Gio.TlsCertificate) -> None:
|
|
63
|
66
|
log.info('Add certificate to trust store')
|
|
64
|
67
|
self._certs.append(certificate)
|
|
65
|
68
|
pem = certificate.props.certificate_pem
|
| ... |
... |
@@ -69,7 +72,10 @@ def add_certificate(self, certificate): |
|
69
|
72
|
self._on_certificate_write_finished,
|
|
70
|
73
|
path)
|
|
71
|
74
|
|
|
72
|
|
- def verify(self, certificate, tls_errors):
|
|
|
75
|
+ def verify(self,
|
|
|
76
|
+ certificate: Gio.TlsCertificate,
|
|
|
77
|
+ tls_errors: set[Gio.TlsCertificateFlags]):
|
|
|
78
|
+
|
|
73
|
79
|
if Gio.TlsCertificateFlags.UNKNOWN_CA in tls_errors:
|
|
74
|
80
|
for trusted_certificate in self._certs:
|
|
75
|
81
|
if trusted_certificate.is_same(certificate):
|
| ... |
... |
@@ -81,9 +87,11 @@ def verify(self, certificate, tls_errors): |
|
81
|
87
|
return False
|
|
82
|
88
|
|
|
83
|
89
|
@staticmethod
|
|
84
|
|
- def _on_certificate_write_finished(data, error, path):
|
|
85
|
|
- if data is None:
|
|
86
|
|
- log.error('Can\'t store certificate: %s', error)
|
|
|
90
|
+ def _on_certificate_write_finished(_successful: bool,
|
|
|
91
|
+ error: Optional[GLib.Error],
|
|
|
92
|
+ path: Path):
|
|
|
93
|
+ if error is not None:
|
|
|
94
|
+ log.error('Can\'t store certificate: %s', error.message)
|
|
87
|
95
|
return
|
|
88
|
96
|
|
|
89
|
97
|
log.info('Certificate stored: %s', path)
|