Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_exp_gre.py @ 4344:95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
rel 455
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 13 Jan 2025 01:23:22 +0100 |
parents | |
children |
rev | line source |
---|---|
4344
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia plugin |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 from abc import ABC, abstractmethod |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from typing import Final, TYPE_CHECKING, Self, Type, cast |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from twisted.internet import defer |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.protocols.jabber import jid, error as jabber_error |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.words.protocols.jabber import xmlstream |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber.xmlstream import XMPPHandler |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.xish import domish |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from wokkel import data_form, disco, iwokkel |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from zope.interface import implementer |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from libervia.backend.core import exceptions |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from libervia.backend.core.constants import Const as C |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from libervia.backend.core.core_types import SatXMPPEntity |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from libervia.backend.core.i18n import _ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 from libervia.backend.core.log import getLogger |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 from libervia.backend.plugins.plugin_xep_0106 import XEP_0106 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 from libervia.backend.tools import xml_tools |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 if TYPE_CHECKING: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 from libervia.backend.core.main import LiberviaBackend |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 log = getLogger(__name__) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 PLUGIN_INFO = { |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 C.PI_NAME: "Gateway Relayer Encryption", |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_IMPORT_NAME: "GRE", |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 C.PI_TYPE: "XEP", |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 C.PI_MODES: C.PLUG_MODE_BOTH, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 C.PI_PROTOCOLS: [], |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 C.PI_DEPENDENCIES: ["XEP-0106"], |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 C.PI_RECOMMENDATIONS: [], |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 C.PI_MAIN: "GRE", |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 C.PI_HANDLER: "yes", |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 C.PI_DESCRIPTION: _( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 "Handle formatting and encryption to support end-to-end encryption with gateways." |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 ), |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 } |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 NS_GRE_PREFIX: Final = "urn:xmpp:gre:" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 NS_GRE: Final = f"{NS_GRE_PREFIX}0" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 NS_GRE_FORMATTER_PREFIX: Final = f"{NS_GRE_PREFIX}formatter:" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 NS_GRE_ENCRYPTER_PREFIX: Final = f"{NS_GRE_PREFIX}encrypter:" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 NS_GRE_DATA: Final = f"{NS_GRE_PREFIX}data" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 IQ_DATA_REQUEST = C.IQ_GET + '/data[@xmlns="' + NS_GRE + '"]' |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 class Formatter(ABC): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 formatters_classes: dict[str, Type[Self]] = {} |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 name: str = "" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 namespace: str = "" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 _instance: Self | None = None |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 def __init_subclass__(cls, **kwargs) -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 Registers the subclass in the formatters dictionary. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 @param kwargs: Additional keyword arguments. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 assert cls.name and cls.namespace, "name and namespace must be set" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 super().__init_subclass__(**kwargs) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 cls.formatters_classes[cls.namespace] = cls |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 def __init__(self, host: "LiberviaBackend") -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 assert self.__class__._instance is None, "Formatter class must be singleton." |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 self.__class__._instance = self |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 self.host = host |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 @classmethod |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 def get_instance(cls) -> Self: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 if cls._instance is None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 raise exceptions.InternalError("Formatter instance should be set.") |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 return cls._instance |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 @abstractmethod |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 async def format( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 self, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 client: SatXMPPEntity, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 recipient_id: str, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 message_elt: domish.Element, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 encryption_data_form: data_form.Form, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 ) -> bytes: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 raise NotImplementedError |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 class Encrypter(ABC): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 encrypters_classes: dict[str, Type[Self]] = {} |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 name: str = "" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 namespace: str = "" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 _instance: Self | None = None |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 def __init_subclass__(cls, **kwargs) -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 Registers the subclass in the encrypters dictionary. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 @param kwargs: Additional keyword arguments. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 assert cls.name and cls.namespace, "name and namespace must be set" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 super().__init_subclass__(**kwargs) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 cls.encrypters_classes[cls.namespace] = cls |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 def __init__(self, host: "LiberviaBackend") -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 assert self.__class__._instance is None, "Encrypter class must be singleton." |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 self.__class__._instance = self |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 self.host = host |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 @classmethod |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 def get_instance(cls) -> Self: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 if cls._instance is None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 raise exceptions.InternalError("Encrypter instance should be set.") |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 return cls._instance |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 @abstractmethod |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 async def encrypt( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 self, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 client: SatXMPPEntity, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 recipient_id: str, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 message_elt: domish.Element, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 formatted_payload: bytes, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 encryption_data_form: data_form.Form, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 ) -> str: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 raise NotImplementedError |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 class GetDataHandler(ABC): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 gre_formatters: list[str] = [] |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 gre_encrypters: list[str] = [] |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 def __init_subclass__(cls, **kwargs): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 super().__init_subclass__(**kwargs) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 if not cls.gre_formatters or not cls.gre_encrypters: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 raise TypeError( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 f'{cls.__name__} must define "gre_formatters" and "gre_encrypters"' |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 @abstractmethod |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 async def on_relayed_encryption_data( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 self, client: SatXMPPEntity, iq_elt: domish.Element, form: data_form.Form |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 ) -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 raise NotImplementedError |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 class GRE: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 namespace = NS_GRE |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 def __init__(self, host: "LiberviaBackend") -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization") |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 self.host = host |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 self._e = cast(XEP_0106, host.plugins["XEP-0106"]) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 self._data_handlers: dict[SatXMPPEntity, GetDataHandler] = {} |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 host.register_namespace("gre", NS_GRE) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 self.host.register_encryption_plugin(self, "Relayed", NS_GRE) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 host.trigger.add("send", self.send_trigger, priority=0) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 def register_get_data_handler( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 self, client: SatXMPPEntity, handler: GetDataHandler |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 ) -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 if client in self._data_handlers: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 raise exceptions.InternalError( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 '"register_get_data_handler" should not be called twice for the same ' |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 "handler." |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 self._data_handlers[client] = handler |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 def _on_component_data_request( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 self, iq_elt: domish.Element, client: SatXMPPEntity |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 ) -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 iq_elt.handled = True |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 defer.ensureDeferred(self.on_component_data_request(client, iq_elt)) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 async def on_component_data_request( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 self, client: SatXMPPEntity, iq_elt: domish.Element |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 ) -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 form = data_form.Form( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 "result", "Relayed Data Encryption", formNamespace=NS_GRE_DATA |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 try: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 handler = self._data_handlers[client] |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 except KeyError: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 pass |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 else: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 await handler.on_relayed_encryption_data(client, iq_elt, form) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 data_elt = iq_result_elt.addElement((NS_GRE, "data")) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 data_elt.addChild(form.toElement()) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 client.send(iq_result_elt) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 async def get_formatter_and_encrypter( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 self, client: SatXMPPEntity, gateway_jid: jid.JID |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 ) -> tuple[Formatter, Encrypter]: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 """Retrieve Formatter and Encrypter instances for given gateway. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 @param client: client session. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 @param gateway_jid: bare jid of the gateway. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 @return: Formatter and Encrypter instances. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 @raise exceptions.FeatureNotFound: No relevant Formatter or Encrypter could be |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 found. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 disco_infos = await self.host.memory.disco.get_infos(client, gateway_jid) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 try: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 formatter_ns = next( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 f for f in disco_infos.features if f.startswith(NS_GRE_FORMATTER_PREFIX) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 encrypter_ns = next( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 f for f in disco_infos.features if f.startswith(NS_GRE_ENCRYPTER_PREFIX) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 formatter_cls = Formatter.formatters_classes[formatter_ns] |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 encrypter_cls = Encrypter.encrypters_classes[encrypter_ns] |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 except StopIteration as e: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 raise exceptions.FeatureNotFound("No relayed encryption found.") from e |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 except KeyError as e: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 raise exceptions.FeatureNotFound( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 "No compatible relayed encryption found." |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 ) from e |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 return formatter_cls.get_instance(), encrypter_cls.get_instance() |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 def get_encrypted_payload( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 self, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 message_elt: domish.Element, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 ) -> str | None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 """Return encrypted payload if any. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 @param message_elt: The message element. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 @return: Encrypted payload if any, None otherwise. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 encrypted_elt = next(message_elt.elements(NS_GRE, "encrypted"), None) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 if encrypted_elt is None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 return None |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 return str(encrypted_elt) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 async def send_trigger( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 self, client: SatXMPPEntity, stanza_elt: domish.Element |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 ) -> bool: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 @param client: Profile session. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 @param stanza: The stanza that is about to be sent. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 @return: Whether the send message flow should continue or not. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 if stanza_elt.name != "message": |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 return True |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 try: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 recipient = jid.JID(stanza_elt["to"]) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 except (jabber_error.StanzaError, RuntimeError, jid.InvalidFormat) as e: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 raise exceptions.InternalError( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 "Message without recipient encountered. Blocking further processing to" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 f" avoid leaking plaintext data: {stanza_elt.toXml()}" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 ) from e |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 recipient_bare_jid = recipient.userhostJID() |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 encryption_session = client.encryption.getSession(recipient_bare_jid) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 if encryption_session is None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 return True |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 if encryption_session["plugin"].namespace != NS_GRE: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 return True |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 # We are in a relayed encryption session. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 encryption_data_form = await self.get_data(client, recipient_bare_jid) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 formatter, encrypter = await self.get_formatter_and_encrypter( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 client, recipient_bare_jid |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 try: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 recipient_id = self._e.unescape(recipient.user) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 except ValueError as e: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 raise exceptions.DataError('"to" attribute is not in expected fomat') from e |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 formatted_payload = await formatter.format( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 client, recipient_id, stanza_elt, encryption_data_form |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 encrypted_payload = await encrypter.encrypt( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 client, recipient_id, stanza_elt, formatted_payload, encryption_data_form |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 for body_elt in list(stanza_elt.elements(None, "body")): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 stanza_elt.children.remove(body_elt) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 for subject_elt in list(stanza_elt.elements(None, "subject")): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 stanza_elt.children.remove(subject_elt) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 encrypted_elt = stanza_elt.addElement( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 (NS_GRE, "encrypted"), content=encrypted_payload |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 encrypted_elt["formatter"] = formatter.namespace |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 encrypted_elt["encrypter"] = encrypter.namespace |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 return True |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 async def get_data( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 self, client: SatXMPPEntity, recipient_jid: jid.JID |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 ) -> data_form.Form: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 """Retrieve relayed encryption data form. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 @param client: Client session. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 @param recipient_id: Bare jid of the entity to whom we want to send encrypted |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 mesasge. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 @return: Found data form, or None if no data form has been found. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 assert recipient_jid.resource is None, "recipient_jid must be a bare jid." |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 iq_elt = client.IQ("get") |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 iq_elt["to"] = recipient_jid.full() |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 data_elt = iq_elt.addElement((NS_GRE, "data")) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 iq_result_elt = await iq_elt.send() |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 try: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 data_elt = next(iq_result_elt.elements(NS_GRE, "data")) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 except StopIteration: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 raise exceptions.DataError( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 f"Relayed data payload is missing: {iq_result_elt.toXml()}" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 form = data_form.findForm(data_elt, NS_GRE_DATA) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 if form is None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 raise exceptions.DataError( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 f"Relayed data form is missing: {iq_result_elt.toXml()}" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 return form |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 async def get_trust_ui( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 self, client: SatXMPPEntity, entity: jid.JID |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 ) -> xml_tools.XMLUI: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 @param client: The client session. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 @param entity: The entity whose device trust levels to manage. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 @return: An XMLUI Dialog to handle trust for given entity. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 """ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 # We just return an enmpty form for now. |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 return xml_tools.XMLUI(C.XMLUI_FORM) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 def get_handler(self, client: SatXMPPEntity) -> XMPPHandler: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 return GREHandler(self) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 @implementer(iwokkel.IDisco) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 class GREHandler(XMPPHandler): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 def __init__(self, plugin_parent: GRE) -> None: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 self.plugin_parent = plugin_parent |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 def connectionInitialized(self): |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 assert self.parent is not None and self.xmlstream is not None |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 if self.parent.is_component: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 self.xmlstream.addObserver( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 IQ_DATA_REQUEST, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 self.plugin_parent._on_component_data_request, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 client=self.parent, |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 ) |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 def getDiscoInfo( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = "" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 ) -> list[disco.DiscoFeature]: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 return [ |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 disco.DiscoFeature(NS_GRE), |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 ] |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 def getDiscoItems( |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 self, requestor: jid.JID, target: jid.JID, nodeIdentifier: str = "" |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
381 ) -> list[disco.DiscoItems]: |
95f8309f86cf
plugin GRE: implements Gateway Relayed Encryption:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 return [] |