1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
########################################################################
# File name: caps390.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import base64
import pathlib
import collections
import urllib.parse
import aioxmpp.hashes
from .common import AbstractKey
from . import xso as caps_xso
def _process_features(features):
"""
Generate the `Features String` from an iterable of features.
:param features: The features to generate the features string from.
:type features: :class:`~collections.abc.Iterable` of :class:`str`
:return: The `Features String`
:rtype: :class:`bytes`
Generate the `Features String` from the given `features` as specified in
:xep:`390`.
"""
parts = [
feature.encode("utf-8")+b"\x1f"
for feature in features
]
parts.sort()
return b"".join(parts)+b"\x1c"
def _process_identity(identity):
category = (identity.category or "").encode("utf-8")+b"\x1f"
type_ = (identity.type_ or "").encode("utf-8")+b"\x1f"
lang = str(identity.lang or "").encode("utf-8")+b"\x1f"
name = (identity.name or "").encode("utf-8")+b"\x1f"
return b"".join([category, type_, lang, name]) + b"\x1e"
def _process_identities(identities):
"""
Generate the `Identities String` from an iterable of identities.
:param identities: The identities to generate the features string from.
:type identities: :class:`~collections.abc.Iterable` of
:class:`~.disco.xso.Identity`
:return: The `Identities String`
:rtype: :class:`bytes`
Generate the `Identities String` from the given `identities` as specified
in :xep:`390`.
"""
parts = [
_process_identity(identity)
for identity in identities
]
parts.sort()
return b"".join(parts)+b"\x1c"
def _process_field(field):
parts = [
(value or "").encode("utf-8") + b"\x1f"
for value in field.values
]
parts.insert(0, field.var.encode("utf-8")+b"\x1f")
return b"".join(parts)+b"\x1e"
def _process_form(form):
parts = [
_process_field(form)
for form in form.fields
]
parts.sort()
return b"".join(parts)+b"\x1d"
def _process_extensions(exts):
"""
Generate the `Extensions String` from an iterable of data forms.
:param exts: The data forms to generate the extensions string from.
:type exts: :class:`~collections.abc.Iterable` of
:class:`~.forms.xso.Data`
:return: The `Extensions String`
:rtype: :class:`bytes`
Generate the `Extensions String` from the given `exts` as specified
in :xep:`390`.
"""
parts = [
_process_form(form)
for form in exts
]
parts.sort()
return b"".join(parts)+b"\x1c"
def _get_hash_input(info):
return b"".join([
_process_features(info.features),
_process_identities(info.identities),
_process_extensions(info.exts)
])
def _calculate_hash(algo, hash_input):
impl = aioxmpp.hashes.hash_from_algo(algo)
impl.update(hash_input)
return impl.digest()
Key = collections.namedtuple("Key", ["algo", "digest"])
class Key(Key, AbstractKey):
@property
def node(self):
return "urn:xmpp:caps#{}.{}".format(
self.algo,
base64.b64encode(self.digest).decode("ascii")
)
@property
def path(self):
encoded = base64.b32encode(
self.digest
).decode("ascii").rstrip("=").lower()
return (pathlib.Path("caps2") /
urllib.parse.quote(self.algo, safe="") /
encoded[:2] /
encoded[2:4] /
"{}.xml".format(encoded[4:]))
def verify(self, info):
if not isinstance(info, bytes):
info = _get_hash_input(info)
digest = _calculate_hash(self.algo, info)
return digest == self.digest
class Implementation:
def __init__(self, algorithms, **kwargs):
super().__init__(**kwargs)
self.__algorithms = algorithms
def extract_keys(self, presence):
if presence.xep0390_caps is None:
return ()
return (
Key(algo, digest)
for algo, digest in presence.xep0390_caps.digests.items()
if aioxmpp.hashes.is_algo_supported(algo)
)
def put_keys(self, keys, presence):
presence.xep0390_caps = caps_xso.Caps390()
presence.xep0390_caps.digests.update({
key.algo: key.digest
for key in keys
})
def calculate_keys(self, query_response):
input = _get_hash_input(query_response)
for algo in self.__algorithms:
yield Key(algo, _calculate_hash(algo, input))
|