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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
import base64
from typing import Dict, Optional, Set, Tuple, cast
import xml.etree.ElementTree as ET
from omemo import EncryptedKeyMaterial, KeyExchange, Message
import x3dh
try:
import xmlschema
except ImportError as e:
raise ImportError(
"Optional dependency xmlschema not found. Please install xmlschema, or install this package using"
" `pip install python-twomemo[xml]`, to use the ElementTree-based XML serialization/parser helpers."
) from e
from .twomemo import NAMESPACE, BundleImpl, ContentImpl, EncryptedKeyMaterialImpl, KeyExchangeImpl
__all__ = [
"serialize_device_list",
"parse_device_list",
"serialize_bundle",
"parse_bundle",
"serialize_message",
"parse_message"
]
NS = f"{{{NAMESPACE}}}"
DEVICE_LIST_SCHEMA = xmlschema.XMLSchema("""<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='urn:xmpp:omemo:2'
xmlns='urn:xmpp:omemo:2'>
<xs:element name='devices'>
<xs:complexType>
<xs:sequence minOccurs='0' maxOccurs='unbounded'>
<xs:element ref='device'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='device'>
<xs:complexType>
<xs:attribute name='id' type='xs:unsignedInt' use='required'/>
<xs:attribute name='label' type='xs:string'/>
</xs:complexType>
</xs:element>
</xs:schema>
""")
BUNDLE_SCHEMA = xmlschema.XMLSchema("""<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='urn:xmpp:omemo:2'
xmlns='urn:xmpp:omemo:2'>
<xs:element name='bundle'>
<xs:complexType>
<xs:all>
<xs:element ref='spk'/>
<xs:element ref='spks'/>
<xs:element ref='ik'/>
<xs:element ref='prekeys'/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name='spk'>
<xs:complexType>
<xs:simpleContent>
<xs:extension base='xs:base64Binary'>
<xs:attribute name='id' type='xs:unsignedInt' use='required'/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name='spks' type='xs:base64Binary'/>
<xs:element name='ik' type='xs:base64Binary'/>
<xs:element name='prekeys'>
<xs:complexType>
<xs:sequence maxOccurs='unbounded'>
<xs:element ref='pk'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='pk'>
<xs:complexType>
<xs:simpleContent>
<xs:extension base='xs:base64Binary'>
<xs:attribute name='id' type='xs:unsignedInt' use='required'/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
""")
MESSAGE_SCHEMA = xmlschema.XMLSchema("""<?xml version='1.0' encoding='UTF-8'?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
targetNamespace='urn:xmpp:omemo:2'
xmlns='urn:xmpp:omemo:2'>
<xs:element name='encrypted'>
<xs:complexType>
<xs:all>
<xs:element ref='header'/>
<xs:element ref='payload' minOccurs='0' maxOccurs='1'/>
</xs:all>
</xs:complexType>
</xs:element>
<xs:element name='payload' type='xs:base64Binary'/>
<xs:element name='header'>
<xs:complexType>
<xs:sequence maxOccurs='unbounded'>
<xs:element ref='keys'/>
</xs:sequence>
<xs:attribute name='sid' type='xs:unsignedInt'/>
</xs:complexType>
</xs:element>
<xs:element name='keys'>
<xs:complexType>
<xs:sequence maxOccurs='unbounded'>
<xs:element ref='key'/>
</xs:sequence>
<xs:attribute name='jid' type='xs:string' use='required'/>
</xs:complexType>
</xs:element>
<xs:element name='key'>
<xs:complexType>
<xs:simpleContent>
<xs:extension base='xs:base64Binary'>
<xs:attribute name='rid' type='xs:unsignedInt' use='required'/>
<xs:attribute name='kex' type='xs:boolean' default='false'/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
""")
def serialize_device_list(device_list: Dict[int, Optional[str]]) -> ET.Element:
"""
Args:
device_list: The device list to serialize. The first entry of each tuple is the device id, and the
second entry is the optional label.
Returns:
The serialized device list as an XML element.
"""
devices_elt = ET.Element(f"{NS}devices")
for device_id, label in device_list.items():
device_elt = ET.SubElement(devices_elt, f"{NS}device")
device_elt.set("id", str(device_id))
if label is not None:
device_elt.set("label", label)
return devices_elt
def parse_device_list(element: ET.Element) -> Dict[int, Optional[str]]:
"""
Args:
element: The XML element to parse the device list from.
Returns:
The extracted device list. The first entry of each tuple is the device id, and the second entry is the
optional label.
Raises:
XMLSchemaValidationError: in case the element does not conform to the XML schema given in the
specification.
"""
DEVICE_LIST_SCHEMA.validate(element)
return {
int(cast(str, device_elt.get("id"))): device_elt.get("label", None)
for device_elt
in element.iter(f"{NS}device")
}
def serialize_bundle(bundle: BundleImpl) -> ET.Element:
"""
Args:
bundle: The bundle to serialize.
Returns:
The serialized bundle as an XML element.
"""
bundle_elt = ET.Element(f"{NS}bundle")
ET.SubElement(
bundle_elt,
f"{NS}spk",
attrib={ "id": str(bundle.signed_pre_key_id) }
).text = base64.b64encode(bundle.bundle.signed_pre_key).decode("ASCII")
ET.SubElement(
bundle_elt,
f"{NS}spks"
).text = base64.b64encode(bundle.bundle.signed_pre_key_sig).decode("ASCII")
ET.SubElement(
bundle_elt,
f"{NS}ik"
).text = base64.b64encode(bundle.bundle.identity_key).decode("ASCII")
prekeys_elt = ET.SubElement(bundle_elt, f"{NS}prekeys")
for pre_key in bundle.bundle.pre_keys:
ET.SubElement(
prekeys_elt,
f"{NS}pk",
attrib={ "id": str(bundle.pre_key_ids[pre_key]) }
).text = base64.b64encode(pre_key).decode("ASCII")
return bundle_elt
def parse_bundle(element: ET.Element, bare_jid: str, device_id: int) -> BundleImpl:
"""
Args:
element: The XML element to parse the bundle from.
bare_jid: The bare JID this bundle belongs to.
device_id: The device id of the specific device this bundle belongs to.
Returns:
The extracted bundle.
Raises:
XMLSchemaValidationError: in case the element does not conform to the XML schema given in the
specification.
"""
BUNDLE_SCHEMA.validate(element)
spk_elt = cast(ET.Element, element.find(f"{NS}spk"))
pk_elts = list(element.iter(f"{NS}pk"))
return BundleImpl(
bare_jid,
device_id,
x3dh.Bundle(
base64.b64decode(cast(str, cast(ET.Element, element.find(f"{NS}ik")).text)),
base64.b64decode(cast(str, spk_elt.text)),
base64.b64decode(cast(str, cast(ET.Element, element.find(f"{NS}spks")).text)),
frozenset(base64.b64decode(cast(str, pk_elt.text)) for pk_elt in pk_elts)
),
int(cast(str, spk_elt.get("id"))),
{ base64.b64decode(cast(str, pk_elt.text)): int(cast(str, pk_elt.get("id"))) for pk_elt in pk_elts }
)
def serialize_message(message: Message) -> ET.Element:
"""
Args:
message: The message to serialize.
Returns:
The serialized message as an XML element.
"""
assert isinstance(message.content, ContentImpl)
encrypted_elt = ET.Element(f"{NS}encrypted")
header_elt = ET.SubElement(encrypted_elt, f"{NS}header", attrib={ "sid": str(message.device_id) })
for bare_jid in frozenset(encrypted_key_material.bare_jid for encrypted_key_material, _ in message.keys):
keys_elt = ET.SubElement(header_elt, f"{NS}keys", attrib={ "jid": bare_jid })
keys = frozenset(key for key in message.keys if key[0].bare_jid == bare_jid)
for encrypted_key_material, key_exchange in keys:
assert isinstance(encrypted_key_material, EncryptedKeyMaterialImpl)
key_elt = ET.SubElement(
keys_elt,
f"{NS}key",
attrib={ "rid": str(encrypted_key_material.device_id) }
)
authenticated_message = encrypted_key_material.serialize()
if key_exchange is None:
key_elt.text = base64.b64encode(authenticated_message).decode("ASCII")
else:
assert isinstance(key_exchange, KeyExchangeImpl)
key_elt.set("kex", "true")
key_elt.text = base64.b64encode(key_exchange.serialize(authenticated_message)).decode("ASCII")
if not message.content.empty:
ET.SubElement(
encrypted_elt,
f"{NS}payload"
).text = base64.b64encode(message.content.ciphertext).decode("ASCII")
return encrypted_elt
def parse_message(element: ET.Element, bare_jid: str) -> Message:
"""
Args:
element: The XML element to parse the message from.
bare_jid: The bare JID of the sender.
Returns:
The extracted message.
Raises:
ValueError: in case there is malformed data not caught be the XML schema validation.
XMLSchemaValidationError: in case the element does not conform to the XML schema given in the
specification.
"""
MESSAGE_SCHEMA.validate(element)
payload_elt = element.find(f"{NS}payload")
keys: Set[Tuple[EncryptedKeyMaterial, Optional[KeyExchange]]] = set()
for keys_elt in element.iter(f"{NS}keys"):
recipient_bare_jid = cast(str, keys_elt.get("jid"))
for key_elt in keys_elt.iter(f"{NS}key"):
recipient_device_id = int(cast(str, key_elt.get("rid")))
content = base64.b64decode(cast(str, key_elt.text))
key_exchange: Optional[KeyExchangeImpl] = None
authenticated_message: bytes
if bool(key_elt.get("kex", False)):
key_exchange, authenticated_message = KeyExchangeImpl.parse(content)
else:
authenticated_message = content
encrypted_key_material = EncryptedKeyMaterialImpl.parse(
authenticated_message,
recipient_bare_jid,
recipient_device_id
)
keys.add((encrypted_key_material, key_exchange))
return Message(
NAMESPACE,
bare_jid,
int(cast(str, cast(ET.Element, element.find(f"{NS}header")).get("sid"))),
(
ContentImpl.make_empty()
if payload_elt is None
else ContentImpl(base64.b64decode(cast(str, payload_elt.text)))
),
frozenset(keys)
)
|