comparison sat/plugins/plugin_xep_0320.py @ 4048:04e21d6c9188

plugin XEP-0320: Use of DTLS-SRTP in Jingle Sessions: rel 421
author Goffi <goffi@goffi.org>
date Mon, 15 May 2023 17:02:40 +0200
parents
children c23cad65ae99
comparison
equal deleted inserted replaced
4047:f0b1279a53c3 4048:04e21d6c9188
1 #!/usr/bin/env python3
2
3
4 # Libervia plugin
5 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
21 from twisted.words.xish import domish
22 from wokkel import disco, iwokkel
23 from zope.interface import implementer
24
25 from sat.core.constants import Const as C
26 from sat.core.i18n import _
27 from sat.core.log import getLogger
28
29
30 log = getLogger(__name__)
31
32 NS_JINGLE_DTLS = "urn:xmpp:jingle:apps:dtls:0"
33
34 PLUGIN_INFO = {
35 C.PI_NAME: "Use of DTLS-SRTP in Jingle Sessions",
36 C.PI_IMPORT_NAME: "XEP-0320",
37 C.PI_TYPE: "XEP",
38 C.PI_MODES: C.PLUG_MODE_BOTH,
39 C.PI_PROTOCOLS: ["XEP-0320"],
40 C.PI_DEPENDENCIES: ["XEP-0176"],
41 C.PI_RECOMMENDATIONS: [],
42 C.PI_MAIN: "XEP_0320",
43 C.PI_HANDLER: "yes",
44 C.PI_DESCRIPTION: _("""Use of DTLS-SRTP with RTP (for e2ee of A/V calls)"""),
45 }
46
47
48 class XEP_0320:
49 def __init__(self, host):
50 log.info(f"plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization")
51 host.trigger.add("XEP-0176_parse_transport", self._parse_transport_trigger)
52 host.trigger.add("XEP-0176_build_transport", self._build_transport_trigger)
53
54 def get_handler(self, client):
55 return XEP_0320_handler()
56
57 def _parse_transport_trigger(
58 self, transport_elt: domish.Element, ice_data: dict
59 ) -> bool:
60 """Parse the <fingerprint> element"""
61 fingerprint_elt = next(
62 transport_elt.elements(NS_JINGLE_DTLS, "fingerprint"), None
63 )
64 if fingerprint_elt is not None:
65 try:
66 ice_data["fingerprint"] = {
67 "hash": fingerprint_elt["hash"],
68 "setup": fingerprint_elt["setup"],
69 "fingerprint": str(fingerprint_elt),
70 }
71 except KeyError as e:
72 log.warning(
73 f"invalid <fingerprint> (attribue {e} is missing): "
74 f"{fingerprint_elt.toXml()})"
75 )
76
77 return True
78
79 def _build_transport_trigger(
80 self, tranport_elt: domish.Element, ice_data: dict
81 ) -> bool:
82 """Build the <fingerprint> element if possible"""
83 try:
84 fingerprint_data = ice_data["fingerprint"]
85 hash_ = fingerprint_data["hash"]
86 setup = fingerprint_data["setup"]
87 fingerprint = fingerprint_data["fingerprint"]
88 except KeyError:
89 pass
90 else:
91 fingerprint_elt = tranport_elt.addElement(
92 (NS_JINGLE_DTLS, "fingerprint"), content=fingerprint
93 )
94 fingerprint_elt["hash"] = hash_
95 fingerprint_elt["setup"] = setup
96
97 return True
98
99
100 @implementer(iwokkel.IDisco)
101 class XEP_0320_handler(XMPPHandler):
102 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
103 return [disco.DiscoFeature(NS_JINGLE_DTLS)]
104
105 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
106 return []