pax_global_header00006660000000000000000000000064143312040030014500gustar00rootroot0000000000000052 comment=bccec70c8b8dc5fe20ae948b36a4204de2460b0f python-omemo-1.0.2/000077500000000000000000000000001433120400300141335ustar00rootroot00000000000000python-omemo-1.0.2/.flake8000066400000000000000000000001651433120400300153100ustar00rootroot00000000000000[flake8] max-line-length = 110 doctests = True ignore = E201,E202,W503 per-file-ignores = omemo/project.py:E203 python-omemo-1.0.2/.github/000077500000000000000000000000001433120400300154735ustar00rootroot00000000000000python-omemo-1.0.2/.github/workflows/000077500000000000000000000000001433120400300175305ustar00rootroot00000000000000python-omemo-1.0.2/.github/workflows/test-on-push.yml000066400000000000000000000024701433120400300226240ustar00rootroot00000000000000name: test-on-push on: [push] permissions: contents: read jobs: build: runs-on: ubuntu-latest strategy: fail-fast: false matrix: python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "pypy-3.9"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} cache: "pip" - name: Install libsodium, a dependency of python-xeddsa run: sudo apt-get install -y libsodium-dev - name: Install/update package management dependencies run: python -m pip install --upgrade pip setuptools wheel - name: Install runtime dependencies run: pip install -r requirements.txt - name: Install test dependencies run: | pip install --upgrade pytest pytest-asyncio pytest-cov mypy pylint flake8 pip install --upgrade twisted twomemo[xml] oldmemo[xml] - name: Type-check using mypy run: mypy --strict --disable-error-code str-bytes-safe omemo/ setup.py tests/ - name: Lint using pylint run: pylint omemo/ setup.py tests/ - name: Format-check using Flake8 run: flake8 omemo/ setup.py tests/ - name: Test using pytest run: pytest --cov=omemo --cov-report term-missing:skip-covered python-omemo-1.0.2/.gitignore000066400000000000000000000001301433120400300161150ustar00rootroot00000000000000dist/ OMEMO.egg-info/ __pycache__/ .pytest_cache/ .mypy_cache/ .coverage docs/_build/ python-omemo-1.0.2/CHANGELOG.md000066400000000000000000000145431433120400300157530ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [1.0.2] - 4th of November, 2022 ### Added - A new exception `BundleNotFound`, raised by `_download_bundle`, to allow differentiation between technical bundle download failures and the simple absence of a bundle ### Changed - A small change in the package detection in setup.py led to a broken PyPI package - Improved download failure exception semantics and removed incorrect raises annotations ## [1.0.1] - 3rd of November, 2022 ### Added - Python 3.11 to the list of supported versions ## [1.0.0] - 1st of November, 2022 ### Changed - Complete rewrite of the library (and dependencies) to use asynchronous and (strictly) typed Python 3. - Support for different versions of [XEP-0384](https://xmpp.org/extensions/xep-0384.html) via separate backend packages. - Mostly following [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) now (except for the date format). ## [0.14.0] - 12th of March, 2022 ### Changed - Adjusted signature of the `SessionManager.encryptRatchetForwardingMessage` method to accept exactly one bare JID + device id pair - Updated the license in the setup.py, which was desync with the LICENSE file - Adjusted the copyright year ### Removed - Removed Python 3.4 from the list of supported Python versions - Removed some mentions of Python 2 from the README ## [0.13.0] - 15th of December 2021 ### Added - Added a method to retrieve the receiving chain length of a single session form the `SessionManager` ### Changed - The storage interface has been modified to use asyncio coroutines instead of callbacks. - All methods of the session manager that returned promises previously have been replaced with equivalent asyncio coroutines. ### Removed - Dropped Python 2 support and removed a bit of Python 2 compatibility clutter - Removed synchronous APIs - Removed thread-based promise implementation in favor of asyncio futures and coroutines ## [0.12.0] - 28th of March, 2020 ### Changed - The `JSONFileStorage` now encodes JIDs using hashes, to avoid various issues regarding the file system. **WARNING, THERE ARE KNOWN ISSUES WITH THIS VERSION OF THE STORAGE IMPLEMENTATION.** - Sending 12 byte IVs instead of 16 now. - `SessionManager.encryptKeyTransportMessage` now receives the optional plaintext as a parameter directly, instead of using a callback to encrypt external data. If the plaintext is omitted, a shared secret is returned that can be used for purposes external to this library. - Switched to MIT license with the agreement of all contributors! ### Fixed - Fixed the way key transport messages are sent and received to be compatible with other implementations. ## [0.11.0] - 13th of December, 2019 ### Added - Added a new method `resetTrust`, which allows going back to the trust level `undecided`. ### Changed - Merged the `trust` and `distrust` methods into `setTrust`. - `Storage.storeTrust` must now be able to handle the value `None` for the `trust` parameter. ### Removed - Removed the `SessionManagerAsyncIO`, in preparation for the big Python 3 update coming soon™. ### Fixed - Fixed a bug in the `SessionManager`, where the contents of a parameter (passed by reference) were modified. ## [0.10.5] - 21st of July, 2019 ### Fixed - Fixed two bugs in the `JSONFileStorage` implementation. - Fixed a type issue in the `__str__` of the `TrustException`. - Fixed a rare bug where sessions uncapable of encrypting messages would be stored. ## [0.10.4] - 1st of February, 2019 ### Added - Added an implementation of the storage using a simple directory structure and JSON files. ### Changed - `RatchetForwardingMessages` do not require the recipient to be trusted any more, as they contain no user-generated content. See #22 for information. - Renamed `UntrustedException` to `TrustException` and added a fourth field `problem` to get the type of problem (untrusted vs. undecided). ## [0.10.3] - 29th of December, 2018 ### Added - Added a method for requesting trust information from the storage at bulk. This can be overridden for better performance. - Added a method for requesting sessions from the storage at bulk. This can be overridden for better performance. - Added a public method to delete an existing session. This is useful to recover from broken sessions. - Added a public method to retrieve a list of all managed JIDs. - Added a stresstest, mostly to test recursion depth. ### Changed - Turned the changelog upside-down, so that the first entry is the newest. - Modified `no_coroutine` in the promise lib to use a loop instead of recursion. - Modified the promise constructor to run promises in threads. ## [0.10.2] - 29th of December, 2018 ### Added - Added methods to retrieve trust information from the `SessionManager`: `getTrustForDevice` and `getTrustForJID`. ### Changed - Introduced an exception hierarchy to allow for more fine-grained exception catching and type checking. - Most exceptions now implement `__str__` to generate human-readable messages. ### Fixed - Fixed a bug that tried to instantiate an object of the wrong class from serialized data. ## [0.10.1] - 27th of December, 2018 ### Added - Added CHANGELOG. ### Changed - Upon serialization the current library version is added to the serialized structures, to allow for seamless updates in the future. [Unreleased]: https://github.com/Syndace/python-omemo/compare/v1.0.2...HEAD [1.0.2]: https://github.com/Syndace/python-omemo/compare/v1.0.1...v1.0.2 [1.0.1]: https://github.com/Syndace/python-omemo/compare/v1.0.0...v1.0.1 [1.0.0]: https://github.com/Syndace/python-omemo/compare/v0.14.0...v1.0.0 [0.14.0]: https://github.com/Syndace/python-omemo/compare/v0.13.0...v0.14.0 [0.13.0]: https://github.com/Syndace/python-omemo/compare/v0.12.0...v0.13.0 [0.12.0]: https://github.com/Syndace/python-omemo/compare/v0.11.0...v0.12.0 [0.11.0]: https://github.com/Syndace/python-omemo/compare/v0.10.5...v0.11.0 [0.10.5]: https://github.com/Syndace/python-omemo/compare/v0.10.4...v0.10.5 [0.10.4]: https://github.com/Syndace/python-omemo/compare/v0.10.3...v0.10.4 [0.10.3]: https://github.com/Syndace/python-omemo/compare/v0.10.2...v0.10.3 [0.10.2]: https://github.com/Syndace/python-omemo/compare/v0.10.1...v0.10.2 [0.10.1]: https://github.com/Syndace/python-omemo/releases/tag/v0.10.1 python-omemo-1.0.2/Exceptions.drawio000066400000000000000000000041211433120400300174610ustar00rootroot000000000000007ZxdV+o4FIZ/DZfOoi0UuDwKOi5BWQdx9NycFdvYRkLSScOXv35SSKElFfWAJKP1QpudlNbnTbKzd6IV52w8v2AgCnvUh7hiV/15xWlXbPHlNsWPxLJYWRpNa2UIGPJXpoxhgF6gNFaldYJ8GOcackoxR1He6FFCoMdzNsAYneWbPVGcf2oEAqgYBh7AqvUf5PNwZW3Wqxv73xAFYfpkqyprxiBtLA1xCHw6y5icTsU5Y5Ty1dV4fgZxAi/lsrrv/JXa9YsxSPh7bvhpXV2NIoCifxfdq/P+7x/B8/DEacmX44v0N4a+ACCLlPGQBpQA3NlYTxmdEB8mH1sVpU2bLqWRMFrC+Aw5X0g1wYRTYQr5GMtaOEf8Prn9r7osPWRq2nP5ycvCIi0QzhaZm5LiQ7Zuc9uylN6nYpLkYjphHtzBJu1ugAWQ72hnr9ol3DIPkCJcQDqG4n1EAwYx4Gia71hA9s9g3W4jobiQKn5A0Vq1VHRfRR2zFLVKRfdVtGaWonap6L6K1o1SVH7uFOCJfFLFdrF4/1MfTcVlkFze9Dq9m87cgxFHlKQNxPMybZSOkZd9FiIOBxFYIpyJJVde4lexTyHjcL4TlKx1UgciF2xpcbZZ/ViutIWZlU/a7uBonZrewdL4g9Fi5cbKZugcfLTY7xwtrlGjxamXku4tacMsSd1S0r0lbZolaaOUdG9JW2ZJ2iwl3VvSNDV1fE0X/WHt5fLnqPXSBqHbmj288JeTUtIPSVrIUFvAX/g2mnNyX0JRbQF/4dtYmpNyX0JSbRF/saSas3JfQlJtYWmxpJrTcl9CUrPCUvnWmbzcAMYxoqQHCAggy2TjNOfdttJulq0772aXC8s3d4jejhUss4ZDubQ8gKZmTXFOubY8gKaGpd7KxeUBNDUs91auLvfX1NaWeyt+bafUdH9Nta2RvJu79uTmtm/fg153xGeNeffu5LsM0y3cB9O4EKreUVrZDgLve/3+JeGQAS8J/s4BwkI93SGgeTFgTSE34JSJwNmguFkntMIMyHeZQP5kvqir80VxKk6XS6jZ7oPD7oazwdNJG5wOfs8vf5WKfkzRQobaFLX6z42Z32PT9i9/et+58Kyzh1LRjylayNCs1FZdcVWnwBtB4pvjqlzj/LurQBuSEaEzcssmMe/CafLXPJqprWM+Sa2p/URqQ6FWcNh3SbANPRRnFphGnvjdBmxXdQNuFiw7EcZDgcVDvgErdQVZUzeyloLsmnYwCtAjhm04RR6MjaPmuLqppecy1AnwGoxhvIRhGrWWdq+Rbmplfa1gguEwwhT4hkTTVtO0ac1SHceKW1t0OJPJaZ/dLNUjSHIQQ4MyONvkatrXKpbqGFbuoItibvR4rWuf52zVO2zYGT5mXe2zna16iR6M4+TP60VUhkhgKrljzna7kl8ZcFdwIcLYEJAAGkKtrtO77jrDrXa3a8rPKRuat/49qnfYdUo6dzxOIGFLZAKOcciO6hR2nUJWkLWFT0hCfgPG5ja0o/qCXed8c9GpPIhpHi3t878aJvQnjxh5bcDBJfEoiUVfg8RbGMeuccyIvth3qm5AoRSHIEouvQnDi1OWpIn527g2bA8Fz33bhdoF7NbtDg9PndzW2XPhPqsMoDiZ36pw7uFJjKYQJ7/PY/KNhzCpIB5byGx79QnTmQJfwOF5sGLepCN4RjFlwkIoSTZbnhDGWyaAUUAS2WCycy8MCWrkAfxDVoyR7y93aoqUzA+Ng/T9rXT+UcPkYv3Uefb/0vmL1kJFnX+dMzw8PHXa/WDn9+H37fxHXWPs2mDOyHdLaQ+QxWCEogj6MhoQoZT+SKC+PfUXTR3NT6K3azM3l+pIe7OhIedRtw9eP9X21kbgKlG5iag+aw9QAVmA+91JkM9kK4qb/3q4rMv870in8x8=python-omemo-1.0.2/LICENSE000066400000000000000000000020711433120400300151400ustar00rootroot00000000000000The MIT License Copyright (c) 2022 Tim Henkes (Syndace) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. python-omemo-1.0.2/MANIFEST.in000066400000000000000000000000271433120400300156700ustar00rootroot00000000000000include omemo/py.typed python-omemo-1.0.2/README.md000066400000000000000000000064711433120400300154220ustar00rootroot00000000000000[![PyPI](https://img.shields.io/pypi/v/OMEMO.svg)](https://pypi.org/project/OMEMO/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/OMEMO.svg)](https://pypi.org/project/OMEMO/) [![Build Status](https://github.com/Syndace/python-omemo/actions/workflows/test-on-push.yml/badge.svg)](https://github.com/Syndace/python-omemo/actions/workflows/test-on-push.yml) TODO: Add doc badge # python-omemo # A Python implementation of the [OMEMO Multi-End Message and Object Encryption protocol](https://xmpp.org/extensions/xep-0384.html). A complete implementation of [XEP-0384](https://xmpp.org/extensions/xep-0384.html) on protocol-level, i.e. more than just the cryptography. python-omemo supports different versions of the specification through so-called backends. A backend for OMEMO in the `urn:xmpp:omemo:2` namespace (the most recent version of the specification) is available in the [python-twomemo](https://github.com/Syndace/python-twomemo) Python package. A backend for (legacy) OMEMO in the `eu.siacs.conversations.axolotl` namespace is available in the [python-oldmemo](https://github.com/Syndace/python-oldmemo) package. Multiple backends can be loaded and used at the same time, the library manages their coexistence transparently. ## Installation ## python-omemo depends on two system libraries, [libxeddsa](https://github.com/Syndace/libxeddsa)>=2,<3 and [libsodium](https://download.libsodium.org/doc/). Install the latest release using pip (`pip install OMEMO`) or manually from source by running `pip install .` (recommended) or `python setup.py install` in the cloned repository. The installation requires libsodium and the Python development headers to be installed. If a locally installed version of libxeddsa is available, [python-xeddsa](https://github.com/Syndace/python-xeddsa) (a dependency of python-omemo) tries to use that. Otherwise it uses prebuilt binaries of the library, which are available for Linux, MacOS and Windows for the amd64 architecture, and potentially for MacOS arm64 too. Set the `LIBXEDDSA_FORCE_LOCAL` environment variable to forbid the usage of prebuilt binaries. ## Testing, Type Checks and Linting ## python-omemo uses [pytest](https://docs.pytest.org/en/latest/) as its testing framework, [mypy](http://mypy-lang.org/) for static type checks and both [pylint](https://pylint.pycqa.org/en/latest/) and [Flake8](https://flake8.pycqa.org/en/latest/) for linting. All tests/checks can be run locally with the following commands: ```sh $ pip install --upgrade pytest pytest-asyncio pytest-cov mypy pylint flake8 $ pip install --upgrade twisted twomemo[xml] oldmemo[xml] $ mypy --strict --disable-error-code str-bytes-safe omemo/ setup.py tests/ $ pylint omemo/ setup.py tests/ $ flake8 omemo/ setup.py tests/ $ pytest --cov=omemo --cov-report term-missing:skip-covered ``` ## Getting Started ## Refer to the documentation on [readthedocs.io](https://python-omemo.readthedocs.io/en/latest/), or build/view it locally in the `docs/` directory. To build the docs locally, install the requirements listed in `docs/requirements.txt`, e.g. using `pip install -r docs/requirements.txt`, and then run `make html` from within the `docs/` directory. The documentation can then be found in `docs/_build/html/`. The `functionality.md` file contains an overview of supported functionality/use cases, mostly targeted at developers. python-omemo-1.0.2/docs/000077500000000000000000000000001433120400300150635ustar00rootroot00000000000000python-omemo-1.0.2/docs/Makefile000066400000000000000000000011331433120400300165210ustar00rootroot00000000000000# Minimal makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build SPHINXPROJ = OMEMO SOURCEDIR = . BUILDDIR = _build # Put it first so that "make" without argument is like "make help". help: @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) .PHONY: help Makefile # Catch-all target: route all unknown targets to Sphinx using the new # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) python-omemo-1.0.2/docs/_static/000077500000000000000000000000001433120400300165115ustar00rootroot00000000000000python-omemo-1.0.2/docs/_static/.gitkeep000066400000000000000000000000001433120400300201300ustar00rootroot00000000000000python-omemo-1.0.2/docs/conf.py000066400000000000000000000065131433120400300163670ustar00rootroot00000000000000# Configuration file for the Sphinx documentation builder. # # This file does only contain a selection of the most common options. For a full list see # the documentation: # http://www.sphinx-doc.org/en/master/config # -- Path setup -------------------------------------------------------------------------- # If extensions (or modules to document with autodoc) are in another directory, add these # directories to sys.path here. If the directory is relative to the documentation root, # use os.path.abspath to make it absolute, like shown here. import os import sys this_file_path = os.path.dirname(os.path.abspath(__file__)) sys.path.append(os.path.join(this_file_path, "..", "omemo")) from version import __version__ as __version from project import project as __project # -- Project information ----------------------------------------------------------------- project = __project["name"] author = __project["author"] copyright = f"{__project['year']}, {__project['author']}" # The short X.Y version version = __version["short"] # The full version, including alpha/beta/rc tags release = __version["full"] # -- General configuration --------------------------------------------------------------- # Add any Sphinx extension module names here, as strings. They can be extensions coming # with Sphinx (named "sphinx.ext.*") or your custom ones. extensions = [ "sphinx.ext.autodoc", "sphinx.ext.viewcode", "sphinx.ext.napoleon", "sphinx_autodoc_typehints" ] # Add any paths that contain templates here, relative to this directory. templates_path = [ "_templates" ] # List of patterns, relative to source directory, that match files and directories to # ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. exclude_patterns = [ "_build", "Thumbs.db", ".DS_Store" ] # -- Options for HTML output ------------------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for a list of # builtin themes. html_theme = "sphinx_rtd_theme" # Add any paths that contain custom static files (such as style sheets) here, relative to # this directory. They are copied after the builtin static files, so a file named # "default.css" will overwrite the builtin "default.css". html_static_path = [ "_static" ] # -- Autodoc Configuration --------------------------------------------------------------- # The following two options seem to be ignored... autodoc_typehints = "description" autodoc_type_aliases = { type_alias: f"{type_alias}" for type_alias in { "Priv", "Seed", "Ed25519Pub", "JSONType" } } def autodoc_skip_member_handler(app, what, name, obj, skip, options): # Skip private members, i.e. those that start with double underscores but do not end in underscores if name.startswith("__") and not name.endswith("_"): return True # Could be achieved using exclude-members, but this is more comfy if name in { "__abstractmethods__", "__dict__", "__module__", "__new__", "__weakref__", "_abc_impl" }: return True # Skip __init__s without documentation. Those are just used for type hints. if name == "__init__" and obj.__doc__ is None: return True return None def setup(app): app.connect("autodoc-skip-member", autodoc_skip_member_handler) python-omemo-1.0.2/docs/getting_started.rst000066400000000000000000000124601433120400300210070ustar00rootroot00000000000000Getting Started =============== python-omemo only ships the core functionality common to all versions of `XEP-0384 `_ and relies on backends to implement the details of each version. Each backend is uniquely identified by the namespace it implements. Backend Selection ----------------- There are two official backends: ================================== ==== Namespace Link ================================== ==== ``urn:xmpp:omemo:2`` `python-twomemo `_ ``eu.siacs.conversations.axolotl`` `python-oldmemo `_ ================================== ==== Both backends (and more) can be loaded at the same time and the library will handle compatibility. You can specify backend priority, which will be used to decide which backend to use for encryption in case a recipient device supports multiple loaded backends. Public API and Backends ----------------------- Backends differ in many aspects, from the wire format of the transferred data to the internal cryptographic primitves used. Thus, most parts of the public API take a parameter that specifies the backend to use for the given operation. The core transparently handles all things common to backends and forwards the backend-specific parts to the corresponding backend. Trust ----- python-omemo offers trust management. Since it is not always obvious how trust and JID/device id/identity key belong together, this section gives an overview of the trust concept followed by python-omemo. Each XMPP account has a pool of identity keys. Each device is assigned one identity key from the pool. Theoretically, this concept allows for one identity key to be assigned to multiple devices, however, the security implications of doing so have not been addressed in the XEP, thus it is not recommended and not supported by this library. Trust levels are assigned to identity keys, not devices. I.e. devices are not directly trusted, only implicitly by trusting the identity key assigned to them. The library works with two types of trust levels: custom trust levels and core trust levels. Custom trust levels are assigned to identity keys and can be any Python string. There is no limitation on the number of custom trust levels. Custom trust levels are not used directly by the library for decisions requiring trust (e.g. during message encryption), instead they are translated to one of the three core trust levels first: Trusted, Distrusted, Undecided. The translation from custom trust levels to core trust levels has to be supplied by implementing the :meth:`~omemo.session_manager.SessionManager._evaluate_custom_trust_level` method. This trust concept allows for the implementation of trust systems like `BTBV `_, `TOFU `_, simple manual trust or more complex systems. Storage ------- python-omemo uses a simple key-value storage to persist its state. This storage has to be provided to the library by implementing the :class:`~omemo.storage.Storage` interface. Refer to the API documentation of the :class:`~omemo.storage.Storage` interface for details. .. WARNING:: It might be tempting to offer a backup/restore flow for the OMEMO data. However, due to the forward secrecy of OMEMO, restoring old data results in broken sessions. It is strongly recommended to not include OMEMO data in backups, and to at most include it in migration flows that make sure that old data can't be restored over newer data. Setting it Up ------------- With the backends selected, the trust system chosen and the storage implementation prepared, the library can be set up. This is done in three steps: 1. Subclass abstract backend classes 2. Subclass abstract core library classes 3. Instantiate the :class:`~omemo.session_manager.SessionManager` Backend Subclassing ^^^^^^^^^^^^^^^^^^^ Create subclasses of the respective backend classes if necessary. Some backends may require you to implement abstract methods, others may not. Refer to the docs of the respective backends for setup instructions. Core Library Subclassing ^^^^^^^^^^^^^^^^^^^^^^^^ Create a subclass of :class:`~omemo.session_manager.SessionManager`. There are various abstract methods for interaction with XMPP (device lists, bundles etc.) and trust management that you have to fill out to integrate the library with your client/framework. The API documentation of the :class:`~omemo.session_manager.SessionManager` class should contain the necessary information. Instantiate the Library ^^^^^^^^^^^^^^^^^^^^^^^ Finally, instantiate the storage, backends and then the :class:`~omemo.session_manager.SessionManager`, which is the class that offers all of the public API for message encryption, decryption, trust and device management etc. To do so, simply call the :meth:`~omemo.session_manager.SessionManager.create` method, passing the backend and storage implementations you've prepared. Refer to the API documentation for details on the configuration options accepted by :meth:`~omemo.session_manager.SessionManager.create`. Migration --------- Refer to :ref:`migration_from_legacy` for information about migrating from pre-stable python-omemo to python-omemo 1.0+. Migrations within stable (1.0+) versions are handled automatically. python-omemo-1.0.2/docs/index.rst000066400000000000000000000022101433120400300167170ustar00rootroot00000000000000OMEMO - A Python implementation of the OMEMO Multi-End Message and Object Encryption protocol. ============================================================================================== A Python implementation of the `OMEMO Multi-End Message and Object Encryption protocol `_. A complete implementation of `XEP-0384 `_ on protocol-level, i.e. more than just the cryptography. python-omemo supports different versions of the specification through so-called backends. A backend for OMEMO in the ``urn:xmpp:omemo:2`` namespace (the most recent version of the specification) is available in the `python-twomemo `_ Python package. A backend for (legacy) OMEMO in the ``eu.siacs.conversations.axolotl`` namespace is available in the `python-oldmemo `_ package. Multiple backends can be loaded and used at the same time, the library manages their coexistence transparently. .. toctree:: installation getting_started migration_from_legacy API Documentation python-omemo-1.0.2/docs/installation.rst000066400000000000000000000015751433120400300203260ustar00rootroot00000000000000Installation ============ python-omemo depends on two system libraries, `libxeddsa `__>=2,<3 and `libsodium `__. Install the latest release using pip (``pip install OMEMO``) or manually from source by running ``pip install .`` (preferred) or ``python setup.py install`` in the cloned repository. The installation requires libsodium and the Python development headers to be installed. If a locally installed version of libxeddsa is available, `python-xeddsa `__ (a dependency of python-omemo) tries to use that. Otherwise it uses prebuilt binaries of the library, which are available for Linux, MacOS and Windows for the amd64 architecture, and potentially for MacOS arm64 too. Set the ``LIBXEDDSA_FORCE_LOCAL`` environment variable to forbid the usage of prebuilt binaries. python-omemo-1.0.2/docs/make.bat000066400000000000000000000014511433120400300164710ustar00rootroot00000000000000@ECHO OFF pushd %~dp0 REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set SOURCEDIR=. set BUILDDIR=_build set SPHINXPROJ=OMEMO if "%1" == "" goto help %SPHINXBUILD% >NUL 2>NUL if errorlevel 9009 ( echo. echo.The 'sphinx-build' command was not found. Make sure you have Sphinx echo.installed, then set the SPHINXBUILD environment variable to point echo.to the full path of the 'sphinx-build' executable. Alternatively you echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from echo.http://sphinx-doc.org/ exit /b 1 ) %SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% goto end :help %SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% :end popd python-omemo-1.0.2/docs/migration_from_legacy.rst000066400000000000000000000007261433120400300221620ustar00rootroot00000000000000.. _migration_from_legacy: Migration from Legacy ===================== Due to the multi-backend approach and storage structure of ``python-omemo``, migrations are provided by backends rather than the core library. For users of legacy ``python-omemo`` (i.e. versions before 1.0.0) with ``python-omemo-backend-signal``, the `python-oldmemo `_ package provides migrations. Please refer to the backend documentation for details. python-omemo-1.0.2/docs/omemo/000077500000000000000000000000001433120400300161775ustar00rootroot00000000000000python-omemo-1.0.2/docs/omemo/backend.rst000066400000000000000000000003001433120400300203110ustar00rootroot00000000000000Module: backend =============== .. automodule:: omemo.backend :members: :special-members: :private-members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/omemo/bundle.rst000066400000000000000000000002751433120400300202060ustar00rootroot00000000000000Module: bundle ============== .. automodule:: omemo.bundle :members: :special-members: :private-members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/omemo/identity_key_pair.rst000066400000000000000000000003361433120400300224470ustar00rootroot00000000000000Module: identity_key_pair ========================= .. automodule:: omemo.identity_key_pair :members: :special-members: :private-members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/omemo/message.rst000066400000000000000000000002241433120400300203530ustar00rootroot00000000000000Module: message =============== .. automodule:: omemo.message :members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/omemo/package.rst000066400000000000000000000004721433120400300203270ustar00rootroot00000000000000Package: omemo ============== .. toctree:: Module: backend Module: bundle Module: identity_key_pair Module: message Module: session Module: session_manager Module: storage Module: types python-omemo-1.0.2/docs/omemo/session.rst000066400000000000000000000002241433120400300204120ustar00rootroot00000000000000Module: session =============== .. automodule:: omemo.session :members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/omemo/session_manager.rst000066400000000000000000000003301433120400300221020ustar00rootroot00000000000000Module: session_manager ======================= .. automodule:: omemo.session_manager :members: :special-members: :private-members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/omemo/storage.rst000066400000000000000000000003111433120400300203700ustar00rootroot00000000000000Module: storage =============== .. automodule:: omemo.storage :members: :special-members: __init__ :private-members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/omemo/types.rst000066400000000000000000000002161433120400300200740ustar00rootroot00000000000000Module: types ============= .. automodule:: omemo.types :members: :undoc-members: :member-order: bysource :show-inheritance: python-omemo-1.0.2/docs/requirements.txt000066400000000000000000000000611433120400300203440ustar00rootroot00000000000000sphinx sphinx-rtd-theme sphinx-autodoc-typehints python-omemo-1.0.2/functionality.md000066400000000000000000000261261433120400300173540ustar00rootroot00000000000000# Functionality/Use Cases # - [x] the maximum number of skipped message keys to keep around per session is configurable - [x] the default is set to 1000 - [x] the value is limited neither upwards nor downwards - [x] skipped message keys are deleted following LRU once the limit is reached - [x] the maximum number of skipped message keys in a single message is configurable - [x] the default is set to the maximum number of skipped message keys per session - [x] the value is not limited downwards, altough a value of 0 is forbidden if the maximum number of skipped message keys per session is non-zero - [x] the value is limited upwards to be lower than or equal to the maximum number of skipped message keys per session - [x] device lists are managed - [x] PEP updates to device lists are handled - [x] a function is provided to feed PEP updates to device lists into the library - [x] a PEP downloading mechanism must be provided by the application to download device lists from PEP - [x] the own device list is managed - [x] the own device is added to the list if it isn't included - [x] a label for the own device can optionally be provided - [x] a PEP publishing mechanism must be provided by the application to update the device list in PEP - [x] the most recent version of all device lists is cached - [x] a convenience method is provided to manually trigger the refresh of a device list - [x] devices that are removed from the device list are marked as "inactive", while devices on the list are marked as "active" - [x] only active devices are considered during encryption - [x] a device becoming inactive has no other effect than it being omitted during encryption, i.e. the data corrsponding to the device is never (automatically) deleted - [x] device lists of different backends are merged. it is assumed that device ids are unique even across backends. the same device id in multiple lists is assumed to represent the same device. - [x] the backends supported by each device are indicated together with the device id - [x] the own bundle is managed - [x] the identity key is managed - [x] the signed pre key is managed - [x] the signed pre key is rotated periodically - [x] the rotation period is configurable - [x] a default rotation period of between one week and one month is provided - [x] the signed pre key of the previous rotation is kept around for the next full period to account for delayed messages - [x] the pre keys are managed - [x] the number of pre keys is capped to 100 - [x] the threshold for when to generate new pre keys is configurable - [x] the threshold can not be configured lower than 25 - [x] the default threshold is 99, which means that every used pre key is replaced right away - [x] a PEP publishing mechanism must be provided by the application to update the bundle in PEP - [x] a PEP downloading mechanism must be provided by the application to download a bundle from PEP - [x] one bundle is managed per backend, only the identity key is shared between all backends/bundles - [x] care is taken to provide the identity key to each backend in the format required by the backend (i.e. Ed25519 or Curve25519) - [x] the own device id is generated - [x] the device id is shared across all backends - [x] the current device lists are consulted prior to device id generation to avoid the very unlikely case of a collision - [x] no efforts are made to detect clashes or avoid races (even on protocol level), due to the very low likeliness of a collision - [x] this mechanism can not prevent collisions with new backends if backends are added after the device id has been generated - [x] it is assumed that other clients also share device ids and identity keys across backends - [x] trust is managed - [x] custom trust levels are supported, allowing for different trust systems like BTBV - [x] a callback must be implemented to translate custom trust levels into core trust levels understood by the library - [x] the default trust level to assign to new devices must be specified - [x] trust decisions are always requested in bulk, such that applications can e.g. show one decision dialog for all outstanding trust decisions - [x] trust is shared across backends - [x] trust is applied to pairs of identity key and bare JID, device ids are not part of trust - [x] sessions can be built - [x] transparently when sending or receiving encrypted messages - [x] explicit session building APIs are not provided - [x] requires own bundle management - [x] sessions are per-backend - [x] a PEP downloading mechanism must be provided by the application to download public bundles from PEP - [x] messages can be encrypted - [x] requires session building, device list management and trust management - [x] multiple recipients are supported - [x] own devices are automatically added to the list of recipients, the sending device is removed from the list - [x] messages are only encrypted for devices whose trust level evaluates to "trusted" - [x] the message is not automatically sent, but a structure containing the encrypted payload and the headers is returned - [x] the backend(s) to encrypt the message with can be selected explicitly or implicitly - [x] in the explicit selection, a list of namespaces is given of which the order decides priority - [x] the type of the message parameter to the encryption methods is generic, and each backend provides a method to serialize the type into a byte string - [x] this is necessary because different backends require different inputs to message encryption. For example, omemo:1 requires stanzas for SCE and legacy OMEMO requires just text - [x] when multiple backends are used together, the generic type can be chosen as the lowest common denominator between all backend input types, and implement the serialization methods accordingly - [x] implicit selection is the default, with the priority order taken from the order of the backends as passed to the constructor - [x] empty OMEMO messages can be sent - [x] transparently when required by the protocol - [x] explicit API for empty OMEMO messages is not provided - [x] a mechanism must be provided by the application to send empty OMEMO messages - [x] trust is not applied for empty OMEMO messages - [x] messages can be decrypted - [x] requires session building and trust management - [x] the whole OMEMO-encrypted message can be passed to the library, it will select the correct header corresponding the the device - [x] the type of the decrypted message is generic, each backend provides a method to deserialize the decrypted message body from a byte string into its respective type - [x] this is necessary because different backends produce different outputs from message decryption. For example, omemo:1 produces stanzas from SCE and legacy OMEMO produces just text - [x] when multiple backends are used together, the generic type can be chosen as the lowest common denominator between all backend output types, and implement the deserialization methods accordingly - [x] device lists are automatically refreshed when encountering a message by a device that is not cached - [x] the backend to decrypt the message with is implicitly selected by looking at the type of the message structure - [x] messages sent by devices with undecided trust are decrytped - [x] it is detectable in case the message of an undecided device was decrypted - [x] duplicate messages are not detected, that task is up to the application - [x] opt-out is not handled - [x] MUC participant list management is not provided - [x] message encryption to multiple recipients is supported though - [x] passive session initiations are automatically completed - [x] requires empty OMEMO messages - [x] message catch-up is handled - [x] methods are provided to notify the library about start and end of message catch-up - [x] the library automatically enters catch-up mode when loaded - [x] pre keys are retained during catch-up and deleted when the catch-up is done - [x] delays automated staleness prevention responses - [x] requires automatic completion of passive session initiations - [x] manual per-device session replacement is provided - [x] requires empty OMEMO messages - [x] global or per-JID session replacement is not provided - [x] own staleness is prevented - [x] received messages with a ratchet counter of 53 or higher trigger an automated response - [x] automated responses are delayed until after catch-up is done and only one message is sent per stale session afterwards - [x] requires empty OMEMO messages - [x] stale devices are not detected - [x] however, API is offered to query the sending chain length of a session, which is one important piece of information that clients might use for staleness detection - [x] account purging is supported - [x] removes all data related to a bare JID across all backends - [x] useful e.g. to remove all OMEMO-related data corresponding to an XMPP account that was blocked by the user - [x] a convenience method to get the identity key fingerprint is provided - [x] independent of the backend - [x] methods are provided to retrieve information about devices - [x] information for all devices of a bare JID can be retrieved in bulk - [x] includes device id, label, identity key, trust information, supported backends, active status - [x] independent of the backend - [x] backends can be provided for different versions of the OMEMO protocol - [x] the protocol version a backend implements is identified by its namespace - [ ] data storage has to be provided by the application - [x] an asyncio-based storage interface has to be implemented - [x] this interface transparently handles caching - [x] the interface represents generic key-value storage with opaque keys and values - [ ] automatic migrations between storage format versions are provided - [x] storage consistency is guaranteed - [x] write operations MUST NOT cache or defer but perform the writing operation right away - [x] when encrypting or decrypting, changes to the state are only persisted when success is guaranteed - [ ] a convenience method to verify consistency (and fix) of the server-side data with the local data is provided - [ ] these checks are not ran automatically, but the documentation includes a hint and examples run the checks after startup # Part of the respective backends - [ ] a state migration tool/function is provided for migration from legacy python-omemo to the new storage format - [ ] a state migration tool/function is provided for migration from libsignal to python-omemo - [ ] convenience functions for XML (de)serialization is provided using the ElementTree API - [ ] this part is fully optional, the application may take care of (de)serialization itself - [ ] installed only when doing `pip install python-omemo-backend-foo[etree]` python-omemo-1.0.2/omemo/000077500000000000000000000000001433120400300152475ustar00rootroot00000000000000python-omemo-1.0.2/omemo/__init__.py000066400000000000000000000042541433120400300173650ustar00rootroot00000000000000from .version import __version__ from .project import project from .backend import Backend, BackendException, DecryptionFailed, KeyExchangeFailed, TooManySkippedMessageKeys from .bundle import Bundle from .message import Content, EncryptedKeyMaterial, KeyExchange, Message from .session_manager import ( SessionManagerException, TrustDecisionFailed, StillUndecided, NoEligibleDevices, MessageNotForUs, SenderNotFound, SenderDistrusted, NoSession, PublicDataInconsistency, UnknownTrustLevel, UnknownNamespace, XMPPInteractionFailed, BundleUploadFailed, BundleDownloadFailed, BundleNotFound, BundleDeletionFailed, DeviceListUploadFailed, DeviceListDownloadFailed, MessageSendingFailed, SessionManager ) from .storage import Just, Maybe, Nothing, NothingException, Storage, StorageException from .types import AsyncFramework, DeviceInformation, JSONType, OMEMOException, TrustLevel # Fun: # https://github.com/PyCQA/pylint/issues/6006 # https://github.com/python/mypy/issues/10198 __all__ = [ # pylint: disable=unused-variable # .version "__version__", # .project "project", # .backend "Backend", "BackendException", "DecryptionFailed", "KeyExchangeFailed", "TooManySkippedMessageKeys", # .bundle "Bundle", # .message "Content", "EncryptedKeyMaterial", "KeyExchange", "Message", # .session_manager "SessionManagerException", "TrustDecisionFailed", "StillUndecided", "NoEligibleDevices", "MessageNotForUs", "SenderNotFound", "SenderDistrusted", "NoSession", "PublicDataInconsistency", "UnknownTrustLevel", "UnknownNamespace", "XMPPInteractionFailed", "BundleUploadFailed", "BundleDownloadFailed", "BundleNotFound", "BundleDeletionFailed", "DeviceListUploadFailed", "DeviceListDownloadFailed", "MessageSendingFailed", "SessionManager", # .storage "Just", "Maybe", "Nothing", "NothingException", "Storage", "StorageException", # .types "AsyncFramework", "DeviceInformation", "JSONType", "OMEMOException", "TrustLevel" ] python-omemo-1.0.2/omemo/backend.py000066400000000000000000000423341433120400300172160ustar00rootroot00000000000000from abc import ABC, abstractmethod from typing import Any, Optional, Tuple from .bundle import Bundle from .message import Content, EncryptedKeyMaterial, PlainKeyMaterial, KeyExchange from .session import Session from .types import OMEMOException __all__ = [ # pylint: disable=unused-variable "Backend", "BackendException", "DecryptionFailed", "KeyExchangeFailed", "TooManySkippedMessageKeys" ] class BackendException(OMEMOException): """ Parent type for all exceptions specific to :class:`Backend`. """ class DecryptionFailed(BackendException): """ Raised by various methods of :class:`Backend` in case of backend-specific failures during decryption. """ class KeyExchangeFailed(BackendException): """ Raised by :meth:`Backend.build_session_active` and :meth:`Backend.build_session_passive` in case of an error during the processing of a key exchange for session building. Known error conditions are: * The bundle does not contain and pre keys (active session building) * The signature of the signed pre key could not be verified (active session building) * An unkown (signed) pre key was referred to (passive session building) Additional backend-specific error conditions might exist. """ class TooManySkippedMessageKeys(BackendException): """ Raised by :meth:`Backend.decrypt_key_material` if a message skips more message keys than allowed. """ class Backend(ABC): """ The base class for all backends. A backend is a unit providing the functionality of a certain OMEMO version to the core library. Warning: Make sure to call :meth:`__init__` from your subclass to configure per-message and per-session skipped message key DoS protection thresholds, and respect those thresholds when decrypting key material using :meth:`decrypt_key_material`. Note: Most methods can raise :class:`~omemo.storage.StorageException` in addition to those exceptions listed explicitly. Note: All usages of "identity key" in the public API refer to the public part of the identity key pair in Ed25519 format. Otherwise, "identity key pair" is explicitly used to refer to the full key pair. Note: For backend implementors: as part of your backend implementation, you are expected to subclass various abstract base classes like :class:`~omemo.session.Session`, :class:`~omemo.message.Content`, :class:`~omemo.message.PlainKeyMaterial`, :class:`~omemo.message.EncryptedKeyMaterial` and :class:`~omemo.message.KeyExchange`. Whenever any of these abstract base types appears in a method signature of the :class:`Backend` class, what's actually meant is an instance of your respective subclass. This is not correctly expressed through the type system, since I couldn't think of a clean way to do so. Adding generics for every single of these types seemed not worth the effort. For now, the recommended way to deal with this type inaccuray is to assert the types of the affected method parameters, for example:: async def store_session(self, session: Session) -> Any: assert isinstance(session, MySessionImpl) ... Doing so tells mypy how to deal with the situation. These assertions should never fail. Note: For backend implementors: you can access the identity key pair at any time via :meth:`omemo.identity_key_pair.IdentityKeyPair.get`. """ def __init__( self, max_num_per_session_skipped_keys: int = 1000, max_num_per_message_skipped_keys: Optional[int] = None ) -> None: """ Args: max_num_per_session_skipped_keys: The maximum number of skipped message keys to keep around per session. Once the maximum is reached, old message keys are deleted to make space for newer ones. Accessible via :attr:`max_num_per_session_skipped_keys`. max_num_per_message_skipped_keys: The maximum number of skipped message keys to accept in a single message. When set to ``None`` (the default), this parameter defaults to the per-session maximum (i.e. the value of the ``max_num_per_session_skipped_keys`` parameter). This parameter may only be 0 if the per-session maximum is 0, otherwise it must be a number between 1 and the per-session maximum. Accessible via :attr:`max_num_per_message_skipped_keys`. """ if max_num_per_message_skipped_keys == 0 and max_num_per_session_skipped_keys != 0: raise ValueError( "The number of allowed per-message skipped keys must be nonzero if the number of per-session" " skipped keys to keep is nonzero." ) if max_num_per_message_skipped_keys or 0 > max_num_per_session_skipped_keys: raise ValueError( "The number of allowed per-message skipped keys must not be greater than the number of" " per-session skipped keys to keep." ) self.__max_num_per_session_skipped_keys = max_num_per_session_skipped_keys self.__max_num_per_message_skipped_keys = max_num_per_session_skipped_keys if \ max_num_per_message_skipped_keys is None else max_num_per_message_skipped_keys @property def max_num_per_session_skipped_keys(self) -> int: """ Returns: The maximum number of skipped message keys to keep around per session. """ return self.__max_num_per_session_skipped_keys @property def max_num_per_message_skipped_keys(self) -> int: """ Returns: The maximum number of skipped message keys to accept in a single message. """ return self.__max_num_per_message_skipped_keys @property @abstractmethod def namespace(self) -> str: """ Returns: The namespace provided/handled by this backend implementation. """ @abstractmethod async def load_session(self, bare_jid: str, device_id: int) -> Optional[Session]: """ Args: bare_jid: The bare JID the device belongs to. device_id: The id of the device. Returns: The session associated with the device, or `None` if such a session does not exist. Warning: Multiple sessions for the same device can exist in memory, however only one session per device can exist in storage. Which one of the in-memory sessions is persisted in storage is controlled by calling the :meth:`store_session` method. """ @abstractmethod async def store_session(self, session: Session) -> Any: """ Store a session, overwriting any previously stored session for the bare JID and device id this session belongs to. Args: session: The session to store. Returns: Anything, the return value is ignored. Warning: Multiple sessions for the same device can exist in memory, however only one session per device can exist in storage. Which one of the in-memory sessions is persisted in storage is controlled by calling this method. """ @abstractmethod async def build_session_active( self, bare_jid: str, device_id: int, bundle: Bundle, plain_key_material: PlainKeyMaterial ) -> Tuple[Session, EncryptedKeyMaterial]: """ Actively build a session. Args: bare_jid: The bare JID the device belongs to. device_id: The id of the device. bundle: The bundle containing the public key material of the other device required for active session building. plain_key_material: The key material to encrypt for the recipient as part of the initial key exchange/session initiation. Returns: The newly built session, the encrypted key material and the key exchange information required by the other device to complete the passive part of session building. The :attr:`~omemo.session.Session.initiation` property of the returned session must return :attr:`~omemo.session.Initiation.ACTIVE`. The :attr:`~omemo.session.Session.key_exchange` property of the returned session must return the information required by the other party to complete its part of the key exchange. Raises: KeyExchangeFailed: in case of failure related to the key exchange required for session building. Warning: This method may be called for a device which already has a session. In that case, the original session must remain in storage and must remain loadable via :meth:`load_session`. Only upon calling :meth:`store_session`, the old session must be overwritten with the new one. In summary, multiple sessions for the same device can exist in memory, while only one session per device can exist in storage, which can be controlled using the :meth:`store_session` method. """ @abstractmethod async def build_session_passive( self, bare_jid: str, device_id: int, key_exchange: KeyExchange, encrypted_key_material: EncryptedKeyMaterial ) -> Tuple[Session, PlainKeyMaterial]: """ Passively build a session. Args: bare_jid: The bare JID the device belongs to. device_id: The id of the device. key_exchange: Key exchange information for the passive session building. encrypted_key_material: The key material to decrypt as part of the initial key exchange/session initiation. Returns: The newly built session and the decrypted key material. Note that the pre key used to initiate this session must somehow be associated with the session, such that :meth:`hide_pre_key` and :meth:`delete_pre_key` can work. Raises: KeyExchangeFailed: in case of failure related to the key exchange required for session building. DecryptionFailed: in case of backend-specific failures during decryption of the initial message. Warning: This method may be called for a device which already has a session. In that case, the original session must remain in storage and must remain loadable via :meth:`load_session`. Only upon calling :meth:`store_session`, the old session must be overwritten with the new one. In summary, multiple sessions for the same device can exist in memory, while only one session per device can exist in storage, which can be controlled using the :meth:`store_session` method. """ @abstractmethod async def encrypt_plaintext(self, plaintext: bytes) -> Tuple[Content, PlainKeyMaterial]: """ Encrypt some plaintext symmetrically. Args: plaintext: The plaintext to encrypt symmetrically. Returns: The encrypted plaintext aka content, as well as the key material needed to decrypt it. """ @abstractmethod async def encrypt_empty(self) -> Tuple[Content, PlainKeyMaterial]: """ Encrypt an empty message for the sole purpose of session manangement/ratchet forwarding/key material transportation. Returns: The symmetrically encrypted empty content, and the key material needed to decrypt it. """ @abstractmethod async def encrypt_key_material( self, session: Session, plain_key_material: PlainKeyMaterial ) -> EncryptedKeyMaterial: """ Encrypt some key material asymmetrically using the session. Args: session: The session to encrypt the key material with. plain_key_material: The key material to encrypt asymmetrically for each recipient. Returns: The encrypted key material. """ @abstractmethod async def decrypt_plaintext(self, content: Content, plain_key_material: PlainKeyMaterial) -> bytes: """ Decrypt some symmetrically encrypted plaintext. Args: content: The content to decrypt. Not empty, i.e. :attr:`Content.empty` will return ``False``. plain_key_material: The key material to decrypt with. Returns: The decrypted plaintext. Raises: DecryptionFailed: in case of backend-specific failures during decryption. """ @abstractmethod async def decrypt_key_material( self, session: Session, encrypted_key_material: EncryptedKeyMaterial ) -> PlainKeyMaterial: """ Decrypt some key material asymmetrically using the session. Args: session: The session to decrypt the key material with. encrypted_key_material: The encrypted key material. Returns: The decrypted key material Raises: TooManySkippedMessageKeys: if the number of message keys skipped by this message exceeds the upper limit enforced by :attr:`max_num_per_message_skipped_keys`. DecryptionFailed: in case of backend-specific failures during decryption. Warning: Make sure to respect the values of :attr:`max_num_per_session_skipped_keys` and :attr:`max_num_per_message_skipped_keys`. Note: When the maximum number of skipped message keys for this session, given by :attr:`max_num_per_session_skipped_keys`, is exceeded, old skipped message keys are deleted to make space for new ones. """ @abstractmethod async def signed_pre_key_age(self) -> int: """ Returns: The age of the signed pre key, i.e. the time elapsed since it was last rotated, in seconds. """ @abstractmethod async def rotate_signed_pre_key(self) -> Any: """ Rotate the signed pre key. Keep the old signed pre key around for one additional rotation period, i.e. until this method is called again. Returns: Anything, the return value is ignored. """ @abstractmethod async def hide_pre_key(self, session: Session) -> bool: """ Hide a pre key from the bundle returned by :meth:`get_bundle` and pre key count returned by :meth:`get_num_visible_pre_keys`, but keep the pre key for cryptographic operations. Args: session: A session that was passively built using :meth:`build_session_passive`. Use this session to identity the pre key to hide. Returns: Whether the pre key was hidden. If the pre key doesn't exist (e.g. because it has already been deleted), or was already hidden, do not throw an exception, but return `False` instead. """ @abstractmethod async def delete_pre_key(self, session: Session) -> bool: """ Delete a pre key. Args: session: A session that was passively built using :meth:`build_session_passive`. Use this session to identity the pre key to delete. Returns: Whether the pre key was deleted. If the pre key doesn't exist (e.g. because it has already been deleted), do not throw an exception, but return `False` instead. """ @abstractmethod async def delete_hidden_pre_keys(self) -> Any: """ Delete all pre keys that were previously hidden using :meth:`hide_pre_key`. Returns: Anything, the return value is ignored. """ @abstractmethod async def get_num_visible_pre_keys(self) -> int: """ Returns: The number of visible pre keys available. The number returned here should match the number of pre keys included in the bundle returned by :meth:`get_bundle`. """ @abstractmethod async def generate_pre_keys(self, num_pre_keys: int) -> Any: """ Generate and store pre keys. Args: num_pre_keys: The number of pre keys to generate. Returns: Anything, the return value is ignored. """ @abstractmethod async def get_bundle(self, bare_jid: str, device_id: int) -> Bundle: """ Args: bare_jid: The bare JID of this XMPP account, to be included in the bundle. device_id: The id of this device, to be included in the bundle. Returns: The bundle containing public information about the cryptographic state of this backend. Warning: Do not include pre keys hidden by :meth:`hide_pre_key` in the bundle! """ @abstractmethod async def purge(self) -> Any: """ Remove all data related to this backend from the storage. Returns: Anything, the return value is ignored. """ @abstractmethod async def purge_bare_jid(self, bare_jid: str) -> Any: """ Delete all data corresponding to an XMPP account. Args: bare_jid: Delete all data corresponding to this bare JID. Returns: Anything, the return value is ignored. """ python-omemo-1.0.2/omemo/bundle.py000066400000000000000000000030651433120400300170760ustar00rootroot00000000000000from abc import ABC, abstractmethod __all__ = [ # pylint: disable=unused-variable "Bundle" ] class Bundle(ABC): """ The bundle of a device, containing the cryptographic information required for active session building. Note: All usages of "identity key" in the public API refer to the public part of the identity key pair in Ed25519 format. """ @property @abstractmethod def namespace(self) -> str: # pylint: disable=missing-function-docstring pass @property @abstractmethod def bare_jid(self) -> str: # pylint: disable=missing-function-docstring pass @property @abstractmethod def device_id(self) -> int: # pylint: disable=missing-function-docstring pass @property @abstractmethod def identity_key(self) -> bytes: # pylint: disable=missing-function-docstring pass @abstractmethod def __eq__(self, other: object) -> bool: """ Check an object for equality with this Bundle instance. Args: other: The object to compare to this instance. Returns: Whether the other object is a bundle with the same contents as this instance. Note: The order in which pre keys are included in the bundles does not matter. """ @abstractmethod def __hash__(self) -> int: """ Hash this instance in a manner that is consistent with :meth:`__eq__`. Returns: An integer value representing this instance. """ python-omemo-1.0.2/omemo/identity_key_pair.py000066400000000000000000000130241433120400300213350ustar00rootroot00000000000000# This import from future (theoretically) enables sphinx_autodoc_typehints to handle type aliases better from __future__ import annotations # pylint: disable=unused-variable from abc import ABC, abstractmethod import logging import secrets import xeddsa.bindings as xeddsa from xeddsa.bindings import Ed25519Pub, Priv, Seed from .storage import NothingException, Storage __all__ = [ # pylint: disable=unused-variable "IdentityKeyPair", "IdentityKeyPairPriv", "IdentityKeyPairSeed" ] class IdentityKeyPair(ABC): """ The identity key pair associated to this device, shared by all backends. There are following requirements for the identity key pair: * It must be able to create and verify Ed25519-compatible signatures. * It must be able to perform X25519-compatible Diffie-Hellman key agreements. There are at least two different kinds of key pairs that can fulfill these requirements: Ed25519 key pairs and Curve25519 key pairs. The birational equivalence of both curves can be used to "convert" one pair to the other. Both types of key pairs share the same private key, however instead of a private key, a seed can be used which the private key is derived from using SHA-512. This is standard practice for Ed25519, where the other 32 bytes of the SHA-512 seed hash are used as a nonce during signing. If a new key pair has to be generated, this implementation generates a seed. Note: This is the only actual cryptographic functionality offered by the core library. Everything else is backend-specific. """ LOG_TAG = "omemo.core.identity_key_pair" @staticmethod async def get(storage: Storage) -> "IdentityKeyPair": """ Get the identity key pair. Args: storage: The storage for all OMEMO-related data. Returns: The identity key pair, which has either been loaded from storage or newly generated. Note: There is only one identity key pair for storage instance. All instances of this class refer to the same storage locations, thus the same data. """ logging.getLogger(IdentityKeyPair.LOG_TAG).debug(f"Creating instance from storage {storage}.") is_seed: bool key: bytes try: # Try to load both is_seed and the key. If any one of the loads fails, generate a new seed. is_seed = (await storage.load_primitive("/ikp/is_seed", bool)).from_just() key = (await storage.load_bytes("/ikp/key")).from_just() logging.getLogger(IdentityKeyPair.LOG_TAG).debug( f"Loaded identity key from storage. is_seed={is_seed}" ) except NothingException: # If there's no private key in storage, generate and store a new seed logging.getLogger(IdentityKeyPair.LOG_TAG).info("Generating identity key.") is_seed = True key = secrets.token_bytes(32) await storage.store("/ikp/is_seed", is_seed) await storage.store_bytes("/ikp/key", key) logging.getLogger(IdentityKeyPair.LOG_TAG).debug("New seed generated and stored.") logging.getLogger(IdentityKeyPair.LOG_TAG).debug("Identity key prepared.") return IdentityKeyPairSeed(key) if is_seed else IdentityKeyPairPriv(key) @property @abstractmethod def is_seed(self) -> bool: """ Returns: Whether this is a :class:`IdentityKeyPairSeed`. """ @property @abstractmethod def is_priv(self) -> bool: """ Returns: Whether this is a :class:`IdentityKeyPairPriv`. """ @abstractmethod def as_priv(self) -> "IdentityKeyPairPriv": """ Returns: An :class:`IdentityKeyPairPriv` derived from this instance (if necessary). """ @property @abstractmethod def identity_key(self) -> Ed25519Pub: """ Returns: The public part of this identity key pair, in Ed25519 format. """ class IdentityKeyPairSeed(IdentityKeyPair): """ An :class:`IdentityKeyPair` represented by a seed. """ def __init__(self, seed: Seed) -> None: """ Args: seed: The Curve25519/Ed25519 seed. """ self.__seed = seed @property def is_seed(self) -> bool: return True @property def is_priv(self) -> bool: return False def as_priv(self) -> "IdentityKeyPairPriv": return IdentityKeyPairPriv(xeddsa.seed_to_priv(self.__seed)) @property def identity_key(self) -> Ed25519Pub: return xeddsa.seed_to_ed25519_pub(self.__seed) @property def seed(self) -> Seed: """ Returns: The Curve25519/Ed25519 seed. """ return self.__seed class IdentityKeyPairPriv(IdentityKeyPair): """ An :class:`IdentityKeyPair` represented by a private key. """ def __init__(self, priv: Priv) -> None: """ Args: priv: The Curve25519/Ed25519 private key. """ self.__priv = priv @property def is_seed(self) -> bool: return False @property def is_priv(self) -> bool: return True def as_priv(self) -> "IdentityKeyPairPriv": return self @property def identity_key(self) -> Ed25519Pub: return xeddsa.priv_to_ed25519_pub(self.__priv) @property def priv(self) -> Priv: """ Returns: The Curve25519/Ed25519 private key. """ return self.__priv python-omemo-1.0.2/omemo/message.py000066400000000000000000000046331433120400300172530ustar00rootroot00000000000000from abc import ABC, abstractmethod from typing import FrozenSet, NamedTuple, Optional, Tuple __all__ = [ # pylint: disable=unused-variable "Content", "EncryptedKeyMaterial", "KeyExchange", "Message", "PlainKeyMaterial" ] class Content(ABC): """ The encrypted content of an OMEMO-encrypted message. Contains for example the ciphertext, but can contain other backend-specific data that is shared between all recipients. """ @property @abstractmethod def empty(self) -> bool: """ Returns: Whether this instance corresponds to an empty OMEMO message purely used for protocol stability reasons. """ class PlainKeyMaterial(ABC): """ Key material which be used to decrypt the content. Defails are backend-specific. """ class EncryptedKeyMaterial(ABC): """ Encrypted key material. When decrypted, the key material can in turn be used to decrypt the content. One collection of key material is included in an OMEMO-encrypted message per recipient. Defails are backend-specific. """ @property @abstractmethod def bare_jid(self) -> str: # pylint: disable=missing-function-docstring pass @property @abstractmethod def device_id(self) -> int: # pylint: disable=missing-function-docstring pass class KeyExchange(ABC): """ Key exchange information, generated by the active part of the session building process, then transferred to and consumed by the passive part of the session building process. Details are backend-specific. """ @property @abstractmethod def identity_key(self) -> bytes: # pylint: disable=missing-function-docstring pass @abstractmethod def builds_same_session(self, other: "KeyExchange") -> bool: """ Args: other: The other key exchange instance to compare to this instance. Returns: Whether the key exchange information stored in this instance and the key exchange information stored in the other instance would build the same session. """ class Message(NamedTuple): # pylint: disable=invalid-name """ Simple structure representing an OMEMO-encrypted message. """ namespace: str bare_jid: str device_id: int content: Content keys: FrozenSet[Tuple[EncryptedKeyMaterial, Optional[KeyExchange]]] python-omemo-1.0.2/omemo/project.py000066400000000000000000000007741433120400300172770ustar00rootroot00000000000000__all__ = [ "project" ] # pylint: disable=unused-variable project = { "name" : "OMEMO", "description" : "A Python implementation of the OMEMO Multi-End Message and Object Encryption protocol.", "url" : "https://github.com/Syndace/python-omemo", "year" : "2022", "author" : "Tim Henkes (Syndace)", "author_email" : "me@syndace.dev", "categories" : [ "Topic :: Communications :: Chat", "Topic :: Security :: Cryptography" ] } python-omemo-1.0.2/omemo/py.typed000066400000000000000000000000001433120400300167340ustar00rootroot00000000000000python-omemo-1.0.2/omemo/session.py000066400000000000000000000070521433120400300173100ustar00rootroot00000000000000from abc import ABC, abstractmethod import enum from typing import Optional from .message import KeyExchange __all__ = [ # pylint: disable=unused-variable "Initiation", "Session" ] @enum.unique class Initiation(enum.Enum): """ Enumeration identifying whether a session was built through active or passive session initiation. """ ACTIVE: str = "ACTIVE" PASSIVE: str = "PASSIVE" class Session(ABC): """ Class representing an OMEMO session. Used to encrypt/decrypt key material for/from a single recipient/sender device in a perfectly forwared secure manner. Warning: Changes to a session may only be persisted when :meth:`~omemo.backend.Backend.store_session` is called. Warning: Multiple sessions for the same device can exist in memory, however only one session per device can exist in storage. Which one of the in-memory sessions is persisted in storage is controlled by calling the :meth:`~omemo.backend.Backend.store_session` method. Note: The API of the :class:`Session` class was intentionally kept thin. All "complex" interactions with session objects happen via methods of :class:`~omemo.backend.Backend`. This allows backend implementors to have the :class:`Session` class be a simple "stupid" data holding structure type, while all of the more complex logic is located in the implementation of the :class:`~omemo.backend.Backend` class itself. Backend implementations are obviously free to implement logic on their respective :class:`Session` implementations and forward calls to them from the :class:`~omemo.backend.Backend` methods. """ @property @abstractmethod def namespace(self) -> str: # pylint: disable=missing-function-docstring pass @property @abstractmethod def bare_jid(self) -> str: # pylint: disable=missing-function-docstring pass @property @abstractmethod def device_id(self) -> int: # pylint: disable=missing-function-docstring pass @property @abstractmethod def initiation(self) -> Initiation: """ Returns: Whether this session was actively initiated or passively. """ @property @abstractmethod def confirmed(self) -> bool: """ In case this session was built through active session initiation, this flag should indicate whether the session initiation has been "confirmed", i.e. at least one message was received and decrypted using this session. """ @property @abstractmethod def key_exchange(self) -> KeyExchange: """ Either the key exchange information received during passive session building, or the key exchange information created as part of active session building. The key exchange information is needed by the protocol for stability reasons, to make sure that all sides can build the session, even if messages are lost or received out of order. Returns: The key exchange information associated with this session. """ @property @abstractmethod def receiving_chain_length(self) -> Optional[int]: """ Returns: The length of the receiving chain, if it exists, used for own staleness detection. """ @property @abstractmethod def sending_chain_length(self) -> int: """ Returns: The length of the sending chain, used for staleness detection of other devices. """ python-omemo-1.0.2/omemo/session_manager.py000066400000000000000000003127171433120400300210110ustar00rootroot00000000000000from abc import ABC, abstractmethod import asyncio import base64 import itertools import logging import secrets from typing import Any, Dict, FrozenSet, List, NamedTuple, Optional, Set, Tuple, Type, TypeVar, Union, cast from typing_extensions import assert_never try: # One of the asynchronous frameworks supported other than asyncio. from twisted.internet import defer, reactor, task, interfaces except ImportError: pass import xeddsa from .backend import Backend, KeyExchangeFailed from .bundle import Bundle from .identity_key_pair import IdentityKeyPair from .message import EncryptedKeyMaterial, KeyExchange, Message, PlainKeyMaterial from .session import Initiation, Session from .storage import NothingException, Storage from .types import AsyncFramework, DeviceInformation, OMEMOException, TrustLevel __all__ = [ # pylint: disable=unused-variable "SessionManagerException", "TrustDecisionFailed", "StillUndecided", "NoEligibleDevices", "MessageNotForUs", "SenderNotFound", "SenderDistrusted", "NoSession", "PublicDataInconsistency", "UnknownTrustLevel", "UnknownNamespace", "XMPPInteractionFailed", "BundleUploadFailed", "BundleDownloadFailed", "BundleNotFound", "BundleDeletionFailed", "DeviceListUploadFailed", "DeviceListDownloadFailed", "MessageSendingFailed", "SessionManager" ] class SessionManagerException(OMEMOException): """ Parent type for all exceptions specific to :class:`SessionManager`. """ class TrustDecisionFailed(SessionManagerException): """ Raised by :meth:`SessionManager._make_trust_decision` if the trust decisions that were queried somehow failed. Indirectly raised by the encryption flow. """ class StillUndecided(SessionManagerException): """ Raised by :meth:`SessionManager.encrypt` in case there are still undecided devices after a trust decision was queried via :meth:`SessionManager._make_trust_decision`. """ class NoEligibleDevices(SessionManagerException): """ Raised by :meth:`SessionManager.encrypt` in case none of the devices of one or more recipient are eligible for encryption, for example due to distrust or bundle downloading failures. """ def __init__(self, bare_jids: FrozenSet[str], *args: object) -> None: """ Args: bare_jids: The JIDs whose devices were not eligible. Accessible as an attribute of the returned instance. """ super().__init__(*args) self.bare_jids = bare_jids class MessageNotForUs(SessionManagerException): """ Raised by :meth:`SessionManager.decrypt` in case the message to decrypt does not seem to be encrypting for this device. """ class SenderNotFound(SessionManagerException): """ Raised by :meth:`SessionManager.decrypt` in case the usual public information of the sending device could not be downloaded. """ class SenderDistrusted(SessionManagerException): """ Raised by :meth:`SessionManager.decrypt` in case the sending device is explicitly distrusted. """ class NoSession(SessionManagerException): """ Raised by :meth:`SessionManager.decrypt` in case there is no session with the sending device, and a new session can't be built either. """ class PublicDataInconsistency(SessionManagerException): """ Raised by :meth:`SessionManager.decrypt` in case inconsistencies were found in the public data of the sending device. """ class UnknownTrustLevel(SessionManagerException): """ Raised by :meth:`SessionManager._evaluate_custom_trust_level` if the custom trust level name to evaluate is unknown. Indirectly raised by the encryption and decryption flows. """ class UnknownNamespace(SessionManagerException): """ Raised by various methods of :class:`SessionManager`, in case the namespace to perform an operation under is not known or the corresponding backend is not currently loaded. """ class XMPPInteractionFailed(SessionManagerException): """ Parent type for all exceptions related to network/XMPP interactions. """ class BundleUploadFailed(XMPPInteractionFailed): """ Raised by :meth:`SessionManager._upload_bundle`, and indirectly by various methods of :class:`SessionManager`. """ class BundleDownloadFailed(XMPPInteractionFailed): """ Raised by :meth:`SessionManager._download_bundle`, and indirectly by various methods of :class:`SessionManager`. """ class BundleNotFound(XMPPInteractionFailed): """ Raised by :meth:`SessionManager._download_bundle`, and indirectly by various methods of :class:`SessionManager`. """ class BundleDeletionFailed(XMPPInteractionFailed): """ Raised by :meth:`SessionManager._delete_bundle`, and indirectly by :meth:`SessionManager.purge_backend`. """ class DeviceListUploadFailed(XMPPInteractionFailed): """ Raised by :meth:`SessionManager._upload_device_list`, and indirectly by various methods of :class:`SessionManager`. """ class DeviceListDownloadFailed(XMPPInteractionFailed): """ Raised by :meth:`SessionManager._download_device_list`, and indirectly by various methods of :class:`SessionManager`. """ class MessageSendingFailed(XMPPInteractionFailed): """ Raised by :meth:`SessionManager._send_message`, and indirectly by various methods of :class:`SessionManager`. """ class EncryptionError(NamedTuple): # pylint: disable=invalid-name """ Structure containing information about an encryption error, returned by :meth:`SessionManager.encrypt`. """ namespace: str bare_jid: str device_id: int exception: Union[BundleDownloadFailed, BundleNotFound, KeyExchangeFailed] SessionManagerTypeT = TypeVar("SessionManagerTypeT", bound="SessionManager") class SessionManager(ABC): """ The core of python-omemo. Manages your own key material and bundle, device lists, sessions with other users and much more, all while being flexibly usable with different backends and transparenlty maintaining a level of compatibility between the backends that allows you to maintain a single identity throughout all of them. Easy APIs are provided to handle common use-cases of OMEMO-enabled XMPP clients, with one of the primary goals being strict type safety. Note: Most methods can raise :class:`~omemo.storage.StorageException` in addition to those exceptions listed explicitly. Note: All parameters are treated as immutable unless explicitly noted otherwise. Note: All usages of "identity key" in the public API refer to the public part of the identity key pair in Ed25519 format. Otherwise, "identity key pair" is explicitly used to refer to the full key pair. Note: The library was designed for use as part of an XMPP library/client. The API is shaped for XMPP and comments/documentation contain references to XEPs and other XMPP-specific nomenclature. However, the library can be used with any economy that provides similar functionality. """ DEVICE_ID_MIN = 1 DEVICE_ID_MAX = 2 ** 31 - 1 STALENESS_MAGIC_NUMBER = 53 LOG_TAG = "omemo.core" def __init__(self) -> None: # Just the type definitions here self.__backends: List[Backend] self.__storage: Storage self.__own_bare_jid: str self.__own_device_id: int self.__undecided_trust_level_name: str self.__pre_key_refill_threshold: int self.__identity_key_pair: IdentityKeyPair self.__synchronizing: bool @classmethod async def create( cls: Type[SessionManagerTypeT], backends: List[Backend], storage: Storage, own_bare_jid: str, initial_own_label: Optional[str], undecided_trust_level_name: str, signed_pre_key_rotation_period: int = 7 * 24 * 60 * 60, pre_key_refill_threshold: int = 99, async_framework: AsyncFramework = AsyncFramework.ASYNCIO ) -> SessionManagerTypeT: """ Load or create OMEMO backends. This method takes care of everything regarding the initialization of OMEMO: generating a unique device id, uploading the bundle and adding the new device to the device list. While doing so, it makes sure that all backends share the same identity key, so that a certain level of compatibility between the backends can be achieved. If a backend was created before, this method loads the backend from the storage instead of creating it. Args: backends: The list of backends to use. storage: The storage for all OMEMO-related data. own_bare_jid: The own bare JID of the account this device belongs to. initial_own_label: The initial (optional) label to assign to this device if supported by any of the backends. undecided_trust_level_name: The name of the custom trust level to initialize the trust level with when a new device is first encoutered. :meth:`_evaluate_custom_trust_level` should evaluate this custom trust level to :attr:`~omemo.types.TrustLevel.UNDECIDED`. signed_pre_key_rotation_period: The rotation period for the signed pre key, in seconds. The rotation period is recommended to be between one week (the default) and one month. pre_key_refill_threshold: The number of pre keys that triggers a refill to 100. Defaults to 99, which means that each pre key gets replaced with a new one right away. The threshold can not be configured to lower than 25. async_framework: The framework to use to create asynchronous tasks and perform asynchronous waiting. Defaults to asyncio, since it's part of the standard library. Make sure the respective framework is installed when using something other than asyncio. Returns: A configured instance of :class:`~omemo.session_manager.SessionManager`, with all backends loaded, bundles published and device lists adjusted. Raises: BundleUploadFailed: if a bundle upload failed. Forwarded from :meth:`_upload_bundle`. BundleDeletionFailed: if a bundle deletion failed. Forwarded from :meth:`_delete_bundle`. DeviceListUploadFailed: if a device list upload failed. Forwarded from :meth:`_upload_device_list`. DeviceListDownloadFailed: if a device list download failed. Forwarded from :meth:`_download_device_list`. Warning: The library starts in history synchronization mode. Call :meth:`after_history_sync` to return to normal operation. Refer to the documentation of :meth:`before_history_sync` and :meth:`after_history_sync` for details. Warning: The library takes care of keeping online data in sync. That means, if the library is loaded without a backend that was loaded before, it will remove all online data related to the missing backend and as much of the offline data as possible (refer to :meth:`purge_backend` for details). Note: This method takes care of leaving the device lists in a consistent state. To do so, backends are "initialized" one after the other. For each backend, the device list is updated as the very last step, after everything else that could fail is done. This ensures that either all data is consistent or the device list does not yet list the inconsistent device. If the creation of one backend succeeds, the data is persisted in the storage before the next backend is created. This guarantees that even if the next backend creation fails, the data is not lost and will be loaded from the storage when calling this method again. Note: The order of the backends can optionally be used by :meth:`encrypt` as the order of priority, in case a recipient device supports multiple backends. Refer to the documentation of :meth:`encrypt` for details. """ if len(frozenset(backend.namespace for backend in backends)) != len(backends): raise ValueError("Multiple backends that handle the same namespace were passed.") if not 25 <= pre_key_refill_threshold <= 99: raise ValueError("Pre key refill threshold out of allowed range.") logging.getLogger(SessionManager.LOG_TAG).debug( f"Preparing library core.\n" f"\tcls={cls}\n" f"\tbackends={backends}\n" f"\tbackend namespaces={[ backend.namespace for backend in backends ]}\n" f"\tstorage={storage}\n" f"\town_bare_jid={own_bare_jid}\n" f"\tinitial_own_label={initial_own_label}\n" f"\tundecided_trust_level_name={undecided_trust_level_name}\n" f"\tsigned_pre_key_rotation_period={signed_pre_key_rotation_period}\n" f"\tpre_key_refill_threshold={pre_key_refill_threshold}" ) self = cls() self.__backends = list(backends) # Copy to make sure the original is not modified self.__storage = storage self.__own_bare_jid = own_bare_jid self.__undecided_trust_level_name = undecided_trust_level_name self.__pre_key_refill_threshold = pre_key_refill_threshold self.__identity_key_pair = await IdentityKeyPair.get(storage) self.__synchronizing = True try: self.__own_device_id = (await self.__storage.load_primitive("/own_device_id", int)).from_just() logging.getLogger(SessionManager.LOG_TAG).debug(f"Device id from storage: {self.__own_device_id}") except NothingException: # First run. logging.getLogger(SessionManager.LOG_TAG).info("First run.") # Fetch the device lists for this bare JID for all loaded backends. device_ids = cast(FrozenSet[int], frozenset()).union(*[ frozenset((await self._download_device_list(backend.namespace, self.__own_bare_jid)).keys()) for backend in self.__backends ]) # Generate a new device id for this device, making sure that it doesn't clash with any of the # existing device ids. self.__own_device_id = next(filter( lambda device_id: device_id not in device_ids, ( secrets.randbelow(cls.DEVICE_ID_MAX - cls.DEVICE_ID_MIN) + cls.DEVICE_ID_MIN for _ in itertools.count() ) )) logging.getLogger(SessionManager.LOG_TAG).debug(f"Generated device id: {self.__own_device_id}") # Store the device information for this device await storage.store( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces", [ backend.namespace for backend in self.__backends ] ) await storage.store( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active", { backend.namespace: True for backend in self.__backends } ) await storage.store( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/label", initial_own_label ) await storage.store_bytes( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/identity_key", self.__identity_key_pair.identity_key ) # Initialize the local device list for this bare JID await storage.store( f"/devices/{self.__own_bare_jid}/list", [ self.__own_device_id ] ) # The trust level of the own identity key doesn't really matter as it's not checked anywhere, but # some value still has to be set such that the device doesn't need special treatment in storage # accessing code. identity_key_b64 = base64.urlsafe_b64encode(self.__identity_key_pair.identity_key) await self.__storage.store( f"/trust/{self.__own_bare_jid}/{identity_key_b64.decode('ASCII')}", undecided_trust_level_name ) # Finally store the device id once the other setup is done await self.__storage.store("/own_device_id", self.__own_device_id) # Generate the first 100 pre keys for each backend for backend in self.__backends: await backend.generate_pre_keys(100) # Publish the bundles for all backends for backend in self.__backends: await self._upload_bundle(await backend.get_bundle(self.__own_bare_jid, self.__own_device_id)) # Trigger a refresh of the own device lists for all backends, this will result in this device # being added to the lists and the lists republished. for backend in self.__backends: await self.refresh_device_list(backend.namespace, self.__own_bare_jid) # If there a mismatch between loaded and active namespaces, look for changes in the loaded backends. device, _ = await self.get_own_device_information() loaded_namespaces = frozenset(backend.namespace for backend in self.__backends) active_namespaces = device.namespaces if loaded_namespaces != active_namespaces: logging.getLogger(SessionManager.LOG_TAG).info( "The list of backends loaded now differs from the list of backends that were loaded last run:" f" {loaded_namespaces} vs. {active_namespaces} (now vs. previous run)" ) # Store the updated list of loaded namespaces await storage.store( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces", list(loaded_namespaces) ) # Set the device active for all loaded namespaces await storage.store( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active", { namespace: True for namespace in loaded_namespaces } ) # Take care of the initialization of newly added backends for backend in self.__backends: if backend.namespace not in active_namespaces: # Refill pre keys if necessary num_visible_pre_keys = await backend.get_num_visible_pre_keys() if num_visible_pre_keys <= self.__pre_key_refill_threshold: await backend.generate_pre_keys(100 - num_visible_pre_keys) # Publish the bundle of the new backend await self._upload_bundle(await backend.get_bundle( self.__own_bare_jid, self.__own_device_id )) # Trigger a refresh of the own device list of the new backend, this will result in this # device being added to the lists and the lists republished. await self.refresh_device_list(backend.namespace, self.__own_bare_jid) # Perform cleanup of removed backends for namespace in active_namespaces - loaded_namespaces: await self.purge_backend(namespace) # Start signed pre key rotation management "in the background" if async_framework is AsyncFramework.ASYNCIO: asyncio.ensure_future(self.__manage_signed_pre_key_rotation( signed_pre_key_rotation_period, async_framework )) elif async_framework is AsyncFramework.TWISTED: defer.ensureDeferred(self.__manage_signed_pre_key_rotation( signed_pre_key_rotation_period, async_framework )) else: assert_never(async_framework) logging.getLogger(SessionManager.LOG_TAG).info( "Library core prepared, entering history synchronization mode." ) return self async def purge_backend(self, namespace: str) -> None: """ Purge a backend, removing both the online data (bundle, device list entry) and the offline data that belongs to this backend. Note that the backend-specific offline data can only be purged if the respective backend is currently loaded. This backend-specific removal can be triggered manually at any time by calling the :meth:`~omemo.backend.Backend.purge` method of the respecfive backend. If the backend to purge is currently loaded, the method will unload it. Args: namespace: The XML namespace managed by the backend to purge. Raises: BundleDeletionFailed: if a bundle deletion failed. Forwarded from :meth:`_delete_bundle`. DeviceListUploadFailed: if a device list upload failed. Forwarded from :meth:`_upload_device_list`. DeviceListDownloadFailed: if a device list download failed. Forwarded from :meth:`_download_device_list`. Warning: Make sure to unsubscribe from updates to all device lists before calling this method. Note: If the backend-specific offline data is not purged, the backend can be loaded again at a later point and the online data can be restored. This is what happens when a backend that was previously loaded is omitted from :meth:`create`. """ logging.getLogger(SessionManager.LOG_TAG).warning(f"Purging backend {namespace}") # First half of online data removal: remove this device from the device list. This has to be the first # step for consistency reasons. device_list = await self._download_device_list(namespace, self.__own_bare_jid) try: device_list.pop(self.__own_device_id) except KeyError: pass else: await self._upload_device_list(namespace, device_list) # Synchronize the offline device list with the online information device, _ = await self.get_own_device_information() active = dict(device.active) active.pop(namespace, None) device = device._replace(namespaces=device.namespaces - frozenset([ namespace ])) device = device._replace(active=frozenset(active.items())) await self.__storage.store( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/namespaces", list(device.namespaces) ) await self.__storage.store( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/active", dict(device.active) ) # If the backend is currently loaded, remove it from the list of loaded backends purged_backend = next(filter(lambda backend: backend.namespace == namespace, self.__backends), None) self.__backends = list(filter(lambda backend: backend.namespace != namespace, self.__backends)) # Remaining backend-specific offline data removal if purged_backend is None: logging.getLogger(SessionManager.LOG_TAG).info( "The backend to purge is not currently loaded. Not purging backend-specific data," " only online data." ) else: logging.getLogger(SessionManager.LOG_TAG).info( "The backend to purge is currently loaded. Purging backend-specific offline data in addition" " to the online data." ) await purged_backend.purge() # Second half of online data removal: delete the bundle of this device. This step has low priority, # thus done last. await self._delete_bundle(namespace, self.__own_device_id) logging.getLogger(SessionManager.LOG_TAG).info(f"Backend {namespace} purged.") async def purge_bare_jid(self, bare_jid: str) -> None: """ Delete all data corresponding to an XMPP account. This includes the device list, trust information and all sessions across all loaded backends. The backend-specific data can be removed at any time by calling the :meth:`~omemo.backend.Backend.purge_bare_jid` method of the respective backend. Args: bare_jid: Delete all data corresponding to this bare JID. """ logging.getLogger(SessionManager.LOG_TAG).warning(f"Purging bare JID {bare_jid}") storage = self.__storage # Get the set of devices to delete device_list = frozenset((await storage.load_list(f"/devices/{bare_jid}/list", int)).maybe([])) # Collect identity keys used by this account identity_keys: Set[bytes] = set() for device_id in device_list: try: identity_keys.add((await storage.load_bytes( f"/devices/{bare_jid}/{device_id}/identity_key" )).from_just()) except NothingException: pass # Delete information about the individual devices for device_id in device_list: await storage.delete(f"/devices/{bare_jid}/{device_id}/namespaces") await storage.delete(f"/devices/{bare_jid}/{device_id}/active") await storage.delete(f"/devices/{bare_jid}/{device_id}/label") await storage.delete(f"/devices/{bare_jid}/{device_id}/identity_key") # Delete the device list await storage.delete(f"/devices/{bare_jid}/list") # Delete information about the identity keys for identity_key in identity_keys: await storage.delete( f"/trust/{bare_jid}/{base64.urlsafe_b64encode(identity_key).decode('ASCII')}" ) # Remove backend-specific data for backend in self.__backends: await backend.purge_bare_jid(bare_jid) logging.getLogger(SessionManager.LOG_TAG).info( f"Bare JID {bare_jid} purged from library core data and backend-specific data of all currently" " loaded backends." ) async def __manage_signed_pre_key_rotation( self, signed_pre_key_rotation_period: int, async_framework: AsyncFramework ) -> None: """ Manage signed pre key rotation. Checks for signed pre keys that are due for rotation, rotates them, uploads the new bundles, and then goes to sleep until the next rotation is due, in an infinite loop. Start this loop "in the background" using ``asyncio.ensure_future``, without ``await``ing the result. Args: signed_pre_key_rotation_period: The rotation period for the signed pre key, in seconds. The rotation period is recommended to be between one week and one month. async_framework: The framework to use to perform asynchronous waiting. Note: If a bundle upload fails after rotating a signed pre key, the method stalls further rotations and instead periodically attempts to upload the bundle. Once the bundle upload succeeds, the method returns to normal operation. """ async def async_sleep(seconds: int) -> None: """ Wait asynchronously using the framework referenced by ``async_framework``. Args: seconds: The number of seconds to wait. """ if async_framework is AsyncFramework.ASYNCIO: await asyncio.sleep(seconds) elif async_framework is AsyncFramework.TWISTED: await task.deferLater(cast(interfaces.IReactorTime, reactor), seconds, lambda: None) else: assert_never(async_framework) while True: # Keep track of when the next signed pre key rotation is due next_check = signed_pre_key_rotation_period # For each backend, check whether the signed pre key is due for rotation for backend in self.__backends: signed_pre_key_age = await backend.signed_pre_key_age() next_rotation = signed_pre_key_rotation_period - signed_pre_key_age logging.getLogger(SessionManager.LOG_TAG).debug( f"Signed pre key age for backend {backend.namespace}: {signed_pre_key_age}. Rotating in" f" {next_rotation} seconds." ) if next_rotation < 0: # Rotate the signed pre if necessary await backend.rotate_signed_pre_key() while True: retry_delay = 60 # Start with one minute of retry delay try: await self._upload_bundle(await backend.get_bundle( self.__own_bare_jid, self.__own_device_id )) except BundleUploadFailed: logging.getLogger(SessionManager.LOG_TAG).error( "Bundle upload failed after rotating signed pre key.", exc_info=True ) await async_sleep(retry_delay) retry_delay += 2 # Double the retry delay retry_delay = min(retry_delay, 60 * 60) # Cap the retry delay at one hour else: break else: # Otherwise, keep track of when the next signed pre key rotation is due next_check = min(next_check, next_rotation) logging.getLogger(SessionManager.LOG_TAG).debug( f"The next signed pre key rotation is due in {next_check} seconds." ) # Add a minute to the delay for the next check next_check += 60 # Wait for the given delay and check again await async_sleep(next_check) async def ensure_data_consistency(self) -> None: """ Ensure that the online data for all loaded backends is consistent with the offline data. Refreshes device lists of all backends while making sure that this device is included in all of them. Downloads the bundle for each backend, compares it with the local bundle contents, and uploads the local bundle if necessary. Raises: DeviceListDownloadFailed: if a device list download failed. Forwarded from :meth:`_download_device_list`. DeviceListUploadFailed: if a device list upload failed. Forwarded from :meth:`update_device_list`. BundleUploadFailed: if a bundle upload failed. Forwarded from :meth:`_upload_bundle`. Note: This method is not called automatically by the library, since under normal working conditions, online and offline data should never desync. However, if clients can spare the network traffic, it is recommended to call this method e.g. once after starting the library and possibly in other scenarios/at regular intervals too. """ logging.getLogger(SessionManager.LOG_TAG).info("Ensuring data consistency.") for backend in self.__backends: await self.refresh_device_list(backend.namespace, self.__own_bare_jid) local_bundle = await backend.get_bundle(self.__own_bare_jid, self.__own_device_id) upload_bundle = False try: remote_bundle = await self._download_bundle( backend.namespace, self.__own_bare_jid, self.__own_device_id ) except (BundleDownloadFailed, BundleNotFound): logging.getLogger(SessionManager.LOG_TAG).warning( "Couldn't download own bundle.", exc_info=True ) upload_bundle = True else: upload_bundle = remote_bundle != local_bundle if upload_bundle: logging.getLogger(SessionManager.LOG_TAG).warning( "Online bundle data differs from offline bundle data." ) await self._upload_bundle(local_bundle) logging.getLogger(SessionManager.LOG_TAG).info("Data consistency ensured/restored.") #################### # abstract methods # #################### @staticmethod @abstractmethod async def _upload_bundle(bundle: Bundle) -> Any: """ Upload the bundle corresponding to this device, overwriting any previously published bundle data. Args: bundle: The bundle to publish. Returns: Anything, the return value is ignored. Raises: UnknownNamespace: if the namespace is unknown. BundleUploadFailed: if the upload failed. Feel free to raise a subclass instead. Note: This method is called from :meth:`create`, before :meth:`create` has returned the instance. Thus, modifications to the object (``self``, in case of subclasses) may not have happened when this method is called. Note: This method must be able to handle at least the namespaces of all loaded backends. """ @staticmethod @abstractmethod async def _download_bundle(namespace: str, bare_jid: str, device_id: int) -> Bundle: """ Download the bundle corresponding to a specific device. Args: namespace: The XML namespace to execute this operation under. bare_jid: The bare JID the device belongs to. device_id: The id of the device. Returns: The bundle. Raises: UnknownNamespace: if the namespace is unknown. BundleDownloadFailed: if the download failed. Feel free to raise a subclass instead. Only raise this on a technical bundle download failure. If the bundle just doesn't exist, raise :class:`BundleNotFound` instead. BundleNotFound: if the bundle doesn't exist. Note: This method is called from :meth:`create`, before :meth:`create` has returned the instance. Thus, modifications to the object (``self``, in case of subclasses) may not have happened when this method is called. Note: This method must be able to handle at least the namespaces of all loaded backends. """ @staticmethod @abstractmethod async def _delete_bundle(namespace: str, device_id: int) -> Any: """ Delete the bundle corresponding to this device. Args: namespace: The XML namespace to execute this operation under. device_id: The id of this device. Returns: Anything, the return value is ignored. Raises: UnknownNamespace: if the namespace is unknown. BundleDeletionFailed: if the deletion failed. Feel free to raise a subclass instead. Only raise this on a technical bundle deletion failure. If the bundle just doesn't exist, don't raise. Note: This method is called from :meth:`create`, before :meth:`create` has returned the instance. Thus, modifications to the object (``self``, in case of subclasses) may not have happened when this method is called. Note: This method must be able to handle at least the namespaces of all loaded backends. In case of backend purging via :meth:`purge_backend`, the corresponding namespace must be supported even if the backend is not currently loaded. """ @staticmethod @abstractmethod async def _upload_device_list(namespace: str, device_list: Dict[int, Optional[str]]) -> Any: """ Upload the device list for this XMPP account. Args: namespace: The XML namespace to execute this operation under. device_list: The device list to upload. Mapping from device id to optional label. Returns: Anything, the return value is ignored. Raises: UnknownNamespace: if the namespace is unknown. DeviceListUploadFailed: if the upload failed. Feel free to raise a subclass instead. Note: This method is called from :meth:`create`, before :meth:`create` has returned the instance. Thus, modifications to the object (``self``, in case of subclasses) may not have happened when this method is called. Note: This method must be able to handle at least the namespaces of all loaded backends. """ @staticmethod @abstractmethod async def _download_device_list(namespace: str, bare_jid: str) -> Dict[int, Optional[str]]: """ Download the device list of a specific XMPP account. Args: namespace: The XML namespace to execute this operation under. bare_jid: The bare JID of the XMPP account. Returns: The device list as a dictionary, mapping the device ids to their optional label. Raises: UnknownNamespace: if the namespace is unknown. DeviceListDownloadFailed: if the download failed. Feel free to raise a subclass instead. Only raise this on a technical device list download failure. If the device list just doesn't exist, return and empty list instead. Note: This method is called from :meth:`create`, before :meth:`create` has returned the instance. Thus, modifications to the object (``self``, in case of subclasses) may not have happened when this method is called. Note: This method must be able to handle at least the namespaces of all loaded backends. """ @abstractmethod async def _evaluate_custom_trust_level(self, device: DeviceInformation) -> TrustLevel: """ Evaluate a custom trust level to one of the three core trust levels: * :attr:`~omemo.types.TrustLevel.TRUSTED`: This device is trusted, encryption/decryption of messages to/from it is allowed. * :attr:`~omemo.types.TrustLevel.DISTRUSTED`: This device is explicitly *not* trusted, do not encrypt/decrypt messages to/from it. * :attr:`~omemo.types.TrustLevel.UNDECIDED`: A trust decision is yet to be made. It is not clear whether it is okay to encrypt messages to it, however decrypting messages from it is allowed. Args: device: Information about the device, including the custom trust level name to translate. Returns: The core trust level corresponding to the custom trust level. Raises: UnknownTrustLevel: if a custom trust level with this name is not known. Feel free to raise a subclass instead. """ @abstractmethod async def _make_trust_decision( self, undecided: FrozenSet[DeviceInformation], identifier: Optional[str] ) -> Any: """ Make a trust decision on a set of undecided identity keys. Args: undecided: A set of devices that require trust decisions. identifier: A piece of application-specific information that callers can pass to :meth:`encrypt`, which is then forwarded here unaltered. This can be used, for example, by instant messaging clients, to identify the chat tab which triggered the call to :meth:`encrypt` and subsequently this call to :meth:`_make_trust_decision`. Returns: Anything, the return value is ignored. The trust decisions are expected to be persisted by calling :meth:`set_trust`. Raises: TrustDecisionFailed: if for any reason the trust decision failed/could not be completed. Feel free to raise a subclass instead. Note: This is called when the encryption needs to know whether it is allowed to encrypt for these devices or not. When this method returns, all previously undecided trust levels should have been replaced by calling :meth:`set_trust` with a different trust level. If they are not replaced or still evaluate to the undecided trust level after the call, the encryption will fail with an exception. See :meth:`encrypt` for details. """ @staticmethod @abstractmethod async def _send_message(message: Message, bare_jid: str) -> Any: """ Send an OMEMO-encrypted message. This is required for various automated behaviours to improve the overall stability of the protocol, for example: * Automatic handshake completion, by responding to incoming key exchanges. * Automatic heartbeat messages to forward the ratchet if many messages were received without a (manual) response, to assure forward secrecy (aka staleness prevention). The number of messages required to trigger this behaviour is hardcoded in :attr:`STALENESS_MAGIC_NUMBER`. * Automatic session initiation if an encrypted message is received but no session exists for that device. * Backend-dependent session healing mechanisms. * Backend-dependent empty messages to notify other devices about potentially "broken" sessions. Note that messages sent here do not contain any content, they just transport key material. Args: message: The message to send. bare_jid: The bare JID to send the message to. Returns: Anything, the return value is ignored. Raises: UnknownNamespace: if the namespace is unknown. MessageSendingFailed: if for any reason the message could not be sent. Feel free to raise a subclass instead. """ ########################## # device list management # ########################## async def update_device_list( self, namespace: str, bare_jid: str, device_list: Dict[int, Optional[str]] ) -> None: """ Update the device list of a specific bare JID, e.g. after receiving an update for the XMPP account from `PEP `__. Args: namespace: The XML namespace to execute this operation under. bare_jid: The bare JID of the XMPP account. device_list: The updated device list. Mapping from device id to optional label. Raises: UnknownNamespace: if the backend to handle the message is not currently loaded. DeviceListUploadFailed: if a device list upload failed. An upload can happen if the device list update is for the own bare JID and does not include the own device. Forwarded from :meth:`_upload_device_list`. """ logging.getLogger(SessionManager.LOG_TAG).debug( f"Incoming device list update:\n" f"\tnamespace={namespace}\n" f"\tbare_jid={bare_jid}\n" f"\tdevice_list={device_list}" ) storage = self.__storage # This isn't strictly necessary, but good for consistency if namespace not in frozenset(backend.namespace for backend in self.__backends): raise UnknownNamespace(f"The backend handling the namespace {namespace} is not currently loaded.") # Copy to make sure the original is not modified device_list = dict(device_list) new_device_list = frozenset(device_list.keys()) old_device_list = frozenset((await storage.load_list(f"/devices/{bare_jid}/list", int)).maybe([])) new_devices = new_device_list - old_device_list logging.getLogger(SessionManager.LOG_TAG).debug(f"Old device list: {old_device_list}") # If the device list is for this JID and a loaded backend, make sure this device is included if ( bare_jid == self.__own_bare_jid and namespace in frozenset(backend.namespace for backend in self.__backends) and self.__own_device_id not in new_device_list ): logging.getLogger(SessionManager.LOG_TAG).warning( "Own device id was not included in the online device list." ) # Add this device to the device list and publish it device_list[self.__own_device_id] = (await storage.load_optional( f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/label", str )).from_just() await self._upload_device_list(namespace, device_list) # Add new device information entries for new devices for device_id in new_devices: await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", [ namespace ]) await storage.store(f"/devices/{bare_jid}/{device_id}/active", { namespace: True }) await storage.store(f"/devices/{bare_jid}/{device_id}/label", device_list[device_id]) # Update namespaces, label and status for previously known devices for device_id in old_device_list: namespaces = set((await storage.load_list( f"/devices/{bare_jid}/{device_id}/namespaces", str )).from_just()) active = (await storage.load_dict(f"/devices/{bare_jid}/{device_id}/active", bool)).from_just() if device_id in device_list: # Add the namespace if required if namespace not in namespaces: namespaces.add(namespace) await storage.store(f"/devices/{bare_jid}/{device_id}/namespaces", list(namespaces)) # Update the status if required if namespace not in active or active[namespace] is False: active[namespace] = True await storage.store(f"/devices/{bare_jid}/{device_id}/active", active) # Update the label if required. Even though loading the value first isn't strictly required, # it is done under the assumption that loading values is cheaper than writing. label = (await storage.load_optional( f"/devices/{bare_jid}/{device_id}/label", str )).from_just() # Don't interpret ``None`` as "no label set" here. Instead, interpret ``None`` as "the backend # doesn't support labels". if device_list[device_id] is not None and device_list[device_id] != label: await storage.store(f"/devices/{bare_jid}/{device_id}/label", device_list[device_id]) else: # Update the status if required if namespace in namespaces: if active[namespace] is True: active[namespace] = False await storage.store(f"/devices/{bare_jid}/{device_id}/active", active) # If there are unknown devices in the new device list, update the list of known devices. Do this as # the last step to ensure data consistency. if len(new_devices) > 0: await storage.store(f"/devices/{bare_jid}/list", list(new_device_list | old_device_list)) logging.getLogger(SessionManager.LOG_TAG).debug("Device list update processed.") async def refresh_device_list(self, namespace: str, bare_jid: str) -> None: """ Manually trigger the refresh of a device list. Args: namespace: The XML namespace to execute this operation under. bare_jid: The bare JID of the XMPP account. Raises: UnknownNamespace: if the namespace is unknown. DeviceListDownloadFailed: if the device list download failed. Forwarded from :meth:`_download_device_list`. DeviceListUploadFailed: if a device list upload failed. An upload can happen if the device list update is for the own bare JID and does not include the own device. Forwarded from :meth:`update_device_list`. """ logging.getLogger(SessionManager.LOG_TAG).debug( f"Device list refresh triggered for namespace {namespace} and bare JID {bare_jid}." ) await self.update_device_list( namespace, bare_jid, await self._download_device_list(namespace, bare_jid) ) #################### # trust management # #################### async def set_trust(self, bare_jid: str, identity_key: bytes, trust_level_name: str) -> None: """ Set the trust level for an identity key. Args: bare_jid: The bare JID of the XMPP account this identity key belongs to. identity_key: The identity key. trust_level_name: The custom trust level to set for the identity key. """ logging.getLogger(SessionManager.LOG_TAG).debug( f"Setting trust level for identity key {identity_key} to {trust_level_name}." ) await self.__storage.store( f"/trust/{bare_jid}/{base64.urlsafe_b64encode(identity_key).decode('ASCII')}", trust_level_name ) ###################### # session management # ###################### async def replace_sessions(self, device: DeviceInformation) -> Dict[str, OMEMOException]: """ Manually replace all sessions for a device. Can be used if sessions are suspected to be broken. This method automatically notifies the other end about the new sessions, so that hopefully no messages are lost. Args: device: The device whose sessions to replace. Returns: Information about exceptions that happened during session replacement attempts. A mapping from the namespace of the backend for which the replacement failed, to the reason of failure. If the reason is a :class:`~omemo.storage.StorageException`, there is a high change that the session was left in an inconsistent state. Other reasons imply that the session replacement failed before having any effect on the state of either side. Warning: This method can not guarantee that sessions are left in a consistent state. For example, if a notification message for the recipient is lost or heavily delayed, the recipient may not know about the new session and keep using the old one. Only use this method to attempt replacement of sessions that already seem broken. Do not attempt to replace healthy sessions. Warning: This method does not optimize towards minimizing network usage. One notification message is sent per session to replace, the notifications are not bundled. This is to minimize the negative impact of network failure. """ logging.getLogger(SessionManager.LOG_TAG).warning(f"Replacing sessions with device {device}.") # The challenge with this method is minimizing the impact of failures at any point. For example, if a # session is replaced and persisted in storage, but sending the corresponding empty message to notify # the recipient about the new session fails, the session in storage will be desync with the session on # the recipient side. Thus, the replacement session is only persisted after the message was # successfully sent. Persisting the new session could fail, resulting in another desync state, however # storage interactions are assumed to be more stable than network interactions. None of this is # failure-proof: the notification message could be lost or heavily delayed, too. However, since this # method is used to replace broken sessions in the first place, a low chance of replacing the broken # session with another broken one doesn't hurt too much. # Do not assume that the given device information is complete and up-to-date. It is okay to use # get_device_information here, since there can only be sessions for devices that have full device # information available. device = next(filter( lambda dev: dev.device_id == device.device_id, await self.get_device_information(device.bare_jid) )) logging.getLogger(SessionManager.LOG_TAG).debug(f"Device information from storage: {device}") # Remove namespaces that correspond to backends which are not currently loaded or backends which have # no session for this device. device = device._replace(namespaces=(device.namespaces & frozenset({ backend.namespace for backend in self.__backends if await backend.load_session(device.bare_jid, device.device_id) is not None }))) unsuccessful: Dict[str, OMEMOException] = {} # Perform the replacement for backend in self.__backends: if backend.namespace in device.namespaces: try: # Prepare an empty message content, plain_key_material = await backend.encrypt_empty() # Build a new session to replace the old one session, encrypted_key_material = await backend.build_session_active( device.bare_jid, device.device_id, await self._download_bundle( backend.namespace, device.bare_jid, device.device_id ), plain_key_material ) # Send the notification message await self._send_message(Message( backend.namespace, self.__own_bare_jid, self.__own_device_id, content, frozenset({ (encrypted_key_material, session.key_exchange) }) ), device.bare_jid) # Store the replacement await backend.store_session(session) except OMEMOException as e: logging.getLogger(SessionManager.LOG_TAG).warning( f"Session replacement failed for namespace {backend.namespace}.", exc_info=True ) unsuccessful[backend.namespace] = e logging.getLogger(SessionManager.LOG_TAG).info("Session replacement done.") return unsuccessful async def get_sending_chain_length(self, device: DeviceInformation) -> Dict[str, Optional[int]]: """ Get the sending chain lengths of all sessions with a device. Can be used for external staleness detection logic. Args: device: The device. Returns: A mapping from namespace to sending chain length. `None` for the sending chain length implies that there is no session with the device for that backend. """ logging.getLogger(SessionManager.LOG_TAG).debug(f"Sending chain length of device {device} requested.") sessions = { backend.namespace: await backend.load_session(device.bare_jid, device.device_id) for backend in self.__backends if backend.namespace in device.namespaces } sending_chain_length = { namespace: None if session is None else session.sending_chain_length for namespace, session in sessions.items() } logging.getLogger(SessionManager.LOG_TAG).debug( f"Sending chain lengths reported by the backends: {sending_chain_length}" ) return sending_chain_length ############################## # device metadata management # ############################## async def set_own_label(self, own_label: Optional[str]) -> None: """ Replace the label for this device, if supported by any of the backends. Args: own_label: The new (optional) label for this device. Raises: DeviceListUploadFailed: if a device list upload failed. Forwarded from :meth:`_upload_device_list`. DeviceListDownloadFailed: if a device list download failed. Forwarded from :meth:`_download_device_list`. Note: It is recommended to keep the length of the label under 53 unicode code points. """ logging.getLogger(SessionManager.LOG_TAG).debug(f"Updating own label to {own_label}.") # Store the new label await self.__storage.store(f"/devices/{self.__own_bare_jid}/{self.__own_device_id}/label", own_label) # For each loaded backend, upload an updated device list including the new label for backend in self.__backends: # Note: it is not required to download the device list here, since it should be cached locally. # However, one PEP node fetch per backend isn't super expensive and it's nice to avoid the code to # load the cached device list. device_list = await self._download_device_list(backend.namespace, self.__own_bare_jid) device_list[self.__own_device_id] = own_label await self._upload_device_list(backend.namespace, device_list) async def get_device_information(self, bare_jid: str) -> FrozenSet[DeviceInformation]: """ Args: bare_jid: Get information about the devices of the XMPP account belonging to this bare JID. Returns: Information about each device of `bare_jid`. The information includes the device id, the identity key, the trust level, whether the device is active and, if supported by any of the backends, the optional label. Returns information about all known devices, regardless of the backend they belong to. Note: Only returns information about cached devices. The cache, however, should be up to date if `PEP `__ updates are correctly fed to :meth:`update_device_list`. A manual update of a device list can be triggered using :meth:`refresh_device_list` if needed. Warning: This method attempts to download the bundle of devices whose corresponding identity key is not known yet. In case the information can not be fetched due to bundle download failures, the device is not included in the returned set. """ # Do not expose the bundle cache publicly. return (await self.__get_device_information(bare_jid))[0] async def __get_device_information( self, bare_jid: str ) -> Tuple[FrozenSet[DeviceInformation], FrozenSet[Bundle]]: """ Internal implementation of :meth:`get_device_information` with the return value extended to include bundles that were downloaded in the process. Args: bare_jid: Get information about the devices of the XMPP account belonging to this bare JID. Returns: Information about each device of `bare_jid`. The information includes the device id, the identity key, the trust level, whether the device is active and, if supported by any of the backends, the optional label. Returns information about all known devices, regardless of the backend they belong to. In the process of gathering this information, it may be necessary to download bundles. Those bundles are returned as well, so that they can be used if required immediately afterwards. This is to avoid double downloading bundles during encryption/decryption flows and is purely for internal use. Warning: This method attempts to download the bundle of devices whose corresponding identity key is not known yet. In case the information can not be fetched due to bundle download failures, the device is not included in the returned set. """ logging.getLogger(SessionManager.LOG_TAG).debug( f"Gathering device information for bare JID {bare_jid}." ) storage = self.__storage device_list = frozenset((await storage.load_list(f"/devices/{bare_jid}/list", int)).maybe([])) logging.getLogger(SessionManager.LOG_TAG).debug(f"Offline device list: {device_list}") devices: Set[DeviceInformation] = set() bundle_cache: Set[Bundle] = set() for device_id in device_list: namespaces = frozenset((await storage.load_list( f"/devices/{bare_jid}/{device_id}/namespaces", str )).from_just()) # Load the identity key as soon as possible, since this is the most likely operation to fail (due # to bundle downloading errors) identity_key: bytes try: identity_key = (await storage.load_bytes( f"/devices/{bare_jid}/{device_id}/identity_key" )).from_just() except NothingException: logging.getLogger(SessionManager.LOG_TAG).debug( f"Identity key assigned to device {device_id} not known." ) # The identity key assigned to this device is not known yet. Fetch the bundle to find that # information. Return the downloaded bundle to avoid double-fetching it if the same bundle is # required for session initiation afterwards. for namespace in namespaces: try: bundle = await self._download_bundle(namespace, bare_jid, device_id) except BundleDownloadFailed: logging.getLogger(SessionManager.LOG_TAG).warning( f"Bundle download failed for device {device_id} of bare JID {bare_jid} for" f" namespace {namespace}.", exc_info=True ) except BundleNotFound: logging.getLogger(SessionManager.LOG_TAG).warning( f"Bundle not available for device {device_id} of bare JID {bare_jid} for" f" namespace {namespace}.", exc_info=True ) else: logging.getLogger(SessionManager.LOG_TAG).debug( f"Identity key information extracted from bundle of namespace {namespace}." ) bundle_cache.add(bundle) identity_key = bundle.identity_key await storage.store_bytes( f"/devices/{bare_jid}/{device_id}/identity_key", identity_key ) break else: # Skip this device in case none of the bundles could be downloaded logging.getLogger(SessionManager.LOG_TAG).warning( f"Not including device {device_id} in the device information set for bare JID" f" {bare_jid} due to the lack of downloadable bundles for identity key assignment." ) continue active = (await storage.load_dict(f"/devices/{bare_jid}/{device_id}/active", bool)).from_just() label = (await storage.load_optional(f"/devices/{bare_jid}/{device_id}/label", str)).from_just() trust_level_name = (await storage.load_primitive( f"/trust/{bare_jid}/{base64.urlsafe_b64encode(identity_key).decode('ASCII')}", str )).maybe(self.__undecided_trust_level_name) devices.add(DeviceInformation( namespaces=namespaces, active=frozenset(active.items()), bare_jid=bare_jid, device_id=device_id, identity_key=identity_key, trust_level_name=trust_level_name, label=label )) logging.getLogger(SessionManager.LOG_TAG).debug("Device information gathered.") return frozenset(devices), frozenset(bundle_cache) async def get_own_device_information(self) -> Tuple[DeviceInformation, FrozenSet[DeviceInformation]]: """ Variation of :meth:`get_device_information` for convenience. Returns: A tuple, where the first entry is information about this device and the second entry contains information about the other devices of the own bare JID. """ all_own_devices = await self.get_device_information(self.__own_bare_jid) other_own_devices = frozenset(filter( lambda dev: dev.device_id != self.__own_device_id, all_own_devices )) return next(iter(all_own_devices - other_own_devices)), other_own_devices @staticmethod def format_identity_key(identity_key: bytes) -> List[str]: """ Args: identity_key: The identity key to generate the fingerprint of. Returns: The fingerprint of the identity key in its Curve25519 form as per the specficiaton, in eight groups of eight lowercase hex chars each. Consider applying `Consistent Color Generation `__ to each individual group when displaying the fingerprint, if applicable. """ ik_hex_string = xeddsa.ed25519_pub_to_curve25519_pub(identity_key).hex() group_size = 8 return [ ik_hex_string[i:i + group_size] for i in range(0, len(ik_hex_string), group_size) ] ########################### # history synchronization # ########################### def before_history_sync(self) -> None: """ Sets the library into "history synchronization mode". In this state, the library assumes that it was offline before and is now running catch-up with whatever happened during the offline phase. Make sure to call :meth:`after_history_sync` when the history synchronization (if any) is done, so that the library can change to normal working behaviour again. The library automatically enters history synchronization mode when loaded via :meth:`create`. Calling this method again when already in history synchronization mode has no effect. Internally, the library does the following things differently during history synchronization: * Pre keys are kept around during history synchronization, to account for the (hopefully rather hypothetical) case that two or more parties selected the same pre key to initiate a session with this device while it was offline. When history synchronization ends, all pre keys that were kept around are deleted and the library returns to normal behaviour. * Empty messages to "complete" sessions or prevent staleness are deferred until after the synchronization is done. Only one empty message is sent per session when exiting the history synchronization mode. Note: While in history synchronization mode, the library can process live events too. """ logging.getLogger(SessionManager.LOG_TAG).info("Entering history synchronization mode.") self.__synchronizing = True async def after_history_sync(self) -> None: """ If the library is in "history synchronization mode" started by :meth:`create` or :meth:`before_history_sync`, calling this makes it return to normal working behaviour. Make sure to call this as soon as history synchronization (if any) is done. Raises: MessageSendingFailed: if one of the queued empty messages could not be sent. Forwarded from :meth:`_send_message`. """ logging.getLogger(SessionManager.LOG_TAG).info("Exiting history synchronization mode.") storage = self.__storage self.__synchronizing = False # Delete pre keys that were hidden while in history synchronization mode for backend in self.__backends: await backend.delete_hidden_pre_keys() # Send empty messages that were queued while in history synchronization mode for backend in self.__backends: # Load and delete the list of bare JIDs that have queued empty messages for this backend queued_jids = frozenset((await storage.load_list(f"/queue/{backend.namespace}", str)).maybe([])) await storage.delete(f"/queue/{backend.namespace}") logging.getLogger(SessionManager.LOG_TAG).debug( f"Bare JIDs for which empty messages are queued for namespace {backend.namespace}:" f" {queued_jids}" ) for bare_jid in queued_jids: # For each queued bare JID, load and delete the list of devices that have queued an empty # message for this backend queued_device_ids = frozenset((await storage.load_list( f"/queue/{backend.namespace}/{bare_jid}", int )).maybe([])) await storage.delete(f"/queue/{backend.namespace}/{bare_jid}") logging.getLogger(SessionManager.LOG_TAG).debug(f"Queued device ids: {queued_device_ids}") for device_id in queued_device_ids: session = await backend.load_session(bare_jid, device_id) if session is None: logging.getLogger(SessionManager.LOG_TAG).warning( f"Can't send queued empty message for device {device_id} of bare JID {bare_jid}" f" for namespace {backend.namespace}. The session could not be loaded." ) else: # It is theoretically possible that the session has been deleted after an empty # message was queued for it. await self.__send_empty_message(backend, session) logging.getLogger(SessionManager.LOG_TAG).debug("History synchronization mode exited.") ###################### # en- and decryption # ###################### async def __send_empty_message(self, backend: Backend, session: Session) -> None: """ Internal helper to send an empty message for ratchet forwarding. Args: backend: The backend to encrypt the message with. session: The session to encrypt the message with. Raises: MessageSendingFailed: if the message could not be sent. Forwarded from :meth:`_send_message`. """ logging.getLogger(SessionManager.LOG_TAG).debug( f"Sending empty message using session {session} ({session.device_id}, {session.bare_jid}," f" {session.namespace}) and backend {backend} ({backend.namespace})." ) content, plain_key_material = await backend.encrypt_empty() encrypted_key_material = await backend.encrypt_key_material(session, plain_key_material) await self._send_message(Message( backend.namespace, self.__own_bare_jid, self.__own_device_id, content, frozenset({ (encrypted_key_material, ( session.key_exchange if session.initiation is Initiation.ACTIVE and not session.confirmed else None )) }) ), session.bare_jid) await backend.store_session(session) async def encrypt( self, bare_jids: FrozenSet[str], plaintext: Dict[str, bytes], backend_priority_order: Optional[List[str]] = None, identifier: Optional[str] = None ) -> Tuple[Dict[Message, PlainKeyMaterial], FrozenSet[EncryptionError]]: """ Encrypt some plaintext for a set of recipients. Args: bare_jids: The bare JIDs of the intended recipients. plaintext: The plaintext to encrypt for the recipients. Since different backends may use different kinds of plaintext, for example just the message body versus a whole stanza using `Stanza Content Encryption `__, this parameter is a dictionary, where the keys are backend namespaces and the values are the plaintext for each specific backend. The plaintext has to be supplied for each backend. backend_priority_order: If a recipient device supports multiple versions of OMEMO, this parameter decides which version to prioritize. If ``None`` is supplied, the order of backends as passed to :meth:`create` is assumed as the order of priority. If a list of namespaces is supplied, the first namespace supported by the recipient is chosen. Lower index means higher priority. identifier: A value that is passed on to :meth:`_make_trust_decision` in case a trust decision is required for any of the recipient devices. This value is not processed or altered, it is simply passed through. Refer to the documentation of :meth:`_make_trust_decision` for details. Returns: A mapping with one message per backend as the keys encrypted for each device of each recipient and for other devices of this account, and the plain key material that was used to encrypt the content of the respective message as values. This plain key material can be used to implement things like legacy OMEMO's KeyTransportMessages. Next to the messages, a set of non-critical errors encountered during encryption are returned. Raises: UnknownNamespace: if the backend priority order list contains a namespace of a backend that is not currently available. UnknownTrustLevel: if an unknown custom trust level name is encountered. Forwarded from :meth:`_evaluate_custom_trust_level`. TrustDecisionFailed: if for any reason the trust decision for undecided devices failed/could not be completed. Forwarded from :meth:`_make_trust_decision`. StillUndecided: if the trust level for one of the recipient devices still evaluates to undecided, even after :meth:`_make_trust_decision` was called to decide on the trust. NoEligibleDevices: if at least one of the intended recipients does not have a single device which qualifies for encryption. Either the recipient does not advertize any OMEMO-enabled devices or all devices were disqualified due to missing trust or failure to download their bundles. KeyExchangeFailed: in case there is an error during the key exchange required for session building. Forwarded from :meth:`~omemo.backend.Backend.build_session_active`. Note: The own JID is implicitly added to the set of recipients, there is no need to list it manually. """ logging.getLogger(SessionManager.LOG_TAG).debug( f"Encrypting plaintext {plaintext} for recipients {bare_jids} with following backend priority" f" order: {backend_priority_order}" ) # Prepare the backend priority order list available_namespaces = [ backend.namespace for backend in self.__backends ] if backend_priority_order is not None: unavailable_namespaces = frozenset(backend_priority_order) - frozenset(available_namespaces) if len(unavailable_namespaces) > 0: raise UnknownNamespace( f"One or more unavailable namespaces were passed in the backend priority order list:" f" {unavailable_namespaces}" ) effective_backend_priority_order = \ available_namespaces if backend_priority_order is None else backend_priority_order logging.getLogger(SessionManager.LOG_TAG).debug( f"Effective backend priority order: {effective_backend_priority_order}" ) # Add the own bare JID to the list of recipients. # Copy to make sure the original is not modified. bare_jids = frozenset(bare_jids) | frozenset({ self.__own_bare_jid }) # Load the device information of all recipients def is_valid_recipient_device(device: DeviceInformation) -> bool: """ Helper that performs various checks to device whether a device is a valid recipient for this encryption operation or not. Excluded are: - this device aka the sending device - devices that are only supported by inactive backends - devices that are only supported by backends which are not in the effective priority order list Args: device: The device to check. Returns: Whether the device is a valid recipient for this encryption operation or not. """ # Remove the own device if device.bare_jid == self.__own_bare_jid and device.device_id == self.__own_device_id: return False # Remove namespaces for which the device is inactive namespaces_active = frozenset(filter( lambda namespace: dict(device.active)[namespace], device.namespaces )) # Remove devices which are only available with backends that are not currently loaded and in # the priority list if len(namespaces_active & frozenset(effective_backend_priority_order)) == 0: return False return True # Using get_device_information here means that some devices may be excluded, if their corresponding # identity key is not known and attempts to download the respecitve bundles fail. Those devices # missing is fine, since get_device_information is the public API for device information anyway, so # the publicly available device list and the recipient devices used here are consistent. tmp = frozenset([ await self.__get_device_information(bare_jid) for bare_jid in bare_jids ]) devices = cast(Set[DeviceInformation], set()).union(*(devices for devices, _ in tmp)) devices = set(filter(is_valid_recipient_device, devices)) bundle_cache = cast(FrozenSet[Bundle], frozenset()).union(*(bundle_cache for _, bundle_cache in tmp)) logging.getLogger(SessionManager.LOG_TAG).debug( f"Recipient devices: {devices}, bundle cache: {bundle_cache}" ) # Apply the backend priority order to the remaining devices def apply_backend_priorty_order( device: DeviceInformation, backend_priority_order: List[str] ) -> DeviceInformation: """ Apply the backend priority order to the namespaces of a device. Args: device: The devices whose namespaces to adjust. backend_priority_order: The backend priority order given as a list of namespaces. Lower index means higher priority. Returns: A copy of the device, with the namespaces adjusted. The set of supported namespaces contains only one namespace - the one with highest priority that is supported by the device. """ return device._replace(namespaces=frozenset(sorted(( namespace for namespace in device.namespaces if dict(device.active)[namespace] and namespace in backend_priority_order ), key=backend_priority_order.index)[0:1])) devices = { apply_backend_priorty_order(device, effective_backend_priority_order) for device in devices } # Remove devices that are not covered by the effective backend priority list devices = { device for device in devices if len(device.namespaces) > 0 } logging.getLogger(SessionManager.LOG_TAG).debug(f"Backend priority order applied: {devices}") # Ask for trust decisions on the remaining devices (or rather, on the identity keys corresponding to # the remaining devices) undecided_devices = frozenset({ device for device in devices if (await self._evaluate_custom_trust_level(device)) is TrustLevel.UNDECIDED }) logging.getLogger(SessionManager.LOG_TAG).debug(f"Undecided devices: {undecided_devices}") if len(undecided_devices) > 0: await self._make_trust_decision(undecided_devices, identifier) # Update to the new trust levels devices = { device._replace(trust_level_name=(await self.__storage.load_primitive( f"/trust/{device.bare_jid}/{base64.urlsafe_b64encode(device.identity_key).decode('ASCII')}", str )).maybe(self.__undecided_trust_level_name)) for device in devices } logging.getLogger(SessionManager.LOG_TAG).debug(f"Updated trust: {devices}") # Make sure the trust status of all previously undecided devices has been decided on undecided_devices = frozenset({ device for device in devices if (await self._evaluate_custom_trust_level(device)) is TrustLevel.UNDECIDED }) if len(undecided_devices) > 0: raise StillUndecided( f"The trust status of one or more devices has not been decided on: {undecided_devices}" ) # Keep only trusted devices devices = { device for device in devices if (await self._evaluate_custom_trust_level(device)) is TrustLevel.TRUSTED } logging.getLogger(SessionManager.LOG_TAG).debug(f"Trusted devices: {devices}") # Encrypt the plaintext once per backend. # About error handling: # - It doesn't matter if a message is encrypted, the corresponding session is stored, and a failure # occurs afterwards. The cryptography/ratchet will not break if that happens. # - Because of the previous point, library-scoped failures like storage failures can crash the whole # encryption process without risking inconsistencies. # - Other than library-scoped failures like storage failures, the only thing that can fail are bundle # fetches/key agreements with new devices. Those failures must not prevent the encryption for the # other devices to fail. Instead, those failures are collected and returned together with the # successful encryption results, such that the library user can decide how to react in detail. messages: Dict[Message, PlainKeyMaterial] = {} encryption_errors: Set[EncryptionError] = set() sessions: Set[Tuple[Backend, Session]] = set() for backend in self.__backends: # Find the devices to encrypt for using this backend backend_devices = frozenset( device for device in devices if next(iter(device.namespaces)) == backend.namespace ) logging.getLogger(SessionManager.LOG_TAG).debug( f"Encrypting for devices {backend_devices} using backend {backend.namespace}." ) # Skip this backend if there isn't a single recipient device using it if len(backend_devices) == 0: continue # Encrypt the message content symmetrically content, plain_key_material = await backend.encrypt_plaintext(plaintext[backend.namespace]) keys: Set[Tuple[EncryptedKeyMaterial, Optional[KeyExchange]]] = set() for device in backend_devices: # Attempt to load the session session = await backend.load_session(device.bare_jid, device.device_id) logging.getLogger(SessionManager.LOG_TAG).debug(f"Session for device {device}: {session}") # Prepare the cached bundle in case it is needed for session building bundle = next((bundle for bundle in bundle_cache if ( bundle.namespace == backend.namespace and bundle.bare_jid == device.bare_jid and bundle.device_id == device.device_id )), None) logging.getLogger(SessionManager.LOG_TAG).debug(f"Cached bundle: {bundle}") try: # Build the session if necessary, and encrypt the key material session, encrypted_key_material = await backend.build_session_active( device.bare_jid, device.device_id, await self._download_bundle( backend.namespace, device.bare_jid, device.device_id ) if bundle is None else bundle, plain_key_material ) if session is None else (session, await backend.encrypt_key_material( session, plain_key_material )) except (BundleDownloadFailed, BundleNotFound, KeyExchangeFailed) as e: # Those failures are non-critical, i.e. encryption for other devices is still performed # and the errors are simply collected and returned. devices.remove(device) encryption_errors.add(EncryptionError( backend.namespace, device.bare_jid, device.device_id, e )) else: # Extract the data that needs to be sent to the other party keys.add((encrypted_key_material, ( session.key_exchange if session.initiation is Initiation.ACTIVE and not session.confirmed else None ))) # Keep track of the modified sessions to store them once encryption is fully done. sessions.add((backend, session)) # Build the message from content, key material and key exchange information messages[Message( backend.namespace, self.__own_bare_jid, self.__own_device_id, content, frozenset(keys) )] = plain_key_material logging.getLogger(SessionManager.LOG_TAG).debug(f"Devices with sessions: {devices}") # Check for recipients without a single remaining device, except for ourselves no_eligible_devices = frozenset(filter( lambda bare_jid: all(device.bare_jid != bare_jid for device in devices), bare_jids )) - frozenset({ self.__own_bare_jid }) if len(no_eligible_devices) > 0: raise NoEligibleDevices( no_eligible_devices, "One or more of the intended recipients does not have a single active and trusted device with" f" a valid session for the loaded backends: {no_eligible_devices}" ) for backend, session in sessions: # Persist the session as the final step await backend.store_session(session) logging.getLogger(SessionManager.LOG_TAG).debug(f"Message encrypted: {messages}") logging.getLogger(SessionManager.LOG_TAG).debug( f"Non-critical encryption errors: {encryption_errors}" ) return messages, frozenset(encryption_errors) async def decrypt(self, message: Message) -> Tuple[Optional[bytes], DeviceInformation, PlainKeyMaterial]: """ Decrypt a message. Args: message: The message to decrypt. Returns: A triple, where the first entry is the decrypted plaintext and the second entry contains information about the device that sent the message. The plaintext is optional and will be ``None`` in case the message was an empty OMEMO message purely used for protocol stability reasons. The third entry is the plain key meterial transported by the message, which can be used to implement functionality like legacy OMEMO's KeyTransportMessages. Raises: UnknownNamespace: if the backend to handle the message is not currently loaded. UnknownTrustLevel: if an unknown custom trust level name is encountered. Forwarded from :meth:`_evaluate_custom_trust_level`. KeyExchangeFailed: in case a new session is built while decrypting this message, and there is an error during the key exchange that's part of the session building. Forwarded from :meth:`~omemo.backend.Backend.build_session_passive`. MessageNotForUs: in case the message does not seem to be encrypted for us. SenderNotFound: in case the public information about the sending device could not be found or is incomplete. SenderDistrusted: in case the identity key corresponding to the sending device is explicitly distrusted. NoSession: in case there is no session with the sending device, and the information required to build a new session is not included either. PublicDataInconsistency: in case there is an inconsistency in the public data of the sending device, which can affect the trust status. MessageSendingFailed: if an attempt to send an empty OMEMO message failed. Forwarded from :meth:`_send_message`. DecryptionFailed: in case of backend-specific failures during decryption. Forwarded from the respective backend implementation. Warning: Do **NOT** implement any automatic reaction to decryption failures, those automatic reactions are transparently handled by the library! *Do* notify the user about decryption failures though, if applicable. Note: If the trust level of the sender evaluates to undecided, the message is decrypted. Note: May send empty OMEMO messages to "complete" key exchanges or prevent staleness. """ logging.getLogger(SessionManager.LOG_TAG).debug(f"Decrypting message: {message}") storage = self.__storage # Find the backend to handle this message backend = next(filter(lambda backend: backend.namespace == message.namespace, self.__backends), None) if backend is None: raise UnknownNamespace( f"Backend corresponding to namespace {message.namespace} is not currently loaded." ) # Check if there is key material for us try: encrypted_key_material, key_exchange = next(filter( lambda k: k[0].bare_jid == self.__own_bare_jid and k[0].device_id == self.__own_device_id, message.keys )) except StopIteration: # pylint: disable=raise-missing-from raise MessageNotForUs("The message to decrypt does not contain key material for us.") logging.getLogger(SessionManager.LOG_TAG).debug( f"Encrypted key material: {encrypted_key_material}, key exchange: {key_exchange}" ) # Check whether the sending device is known devices = await self.get_device_information(message.bare_jid) device = next(filter(lambda device: device.device_id == message.device_id, devices), None) if device is None: logging.getLogger(SessionManager.LOG_TAG).warning( "Sender device is not known, triggering a device list update." ) # If it isn't, trigger a refresh of the device list. This shouldn't be necessary due to PEP # subscription mechanisms, however there might be race conditions and it doesn't hurt to refresh # here. await self.refresh_device_list(message.namespace, message.bare_jid) # Once the device list has been refreshed, look for the device again devices = await self.get_device_information(message.bare_jid) # This time, if the device is still not found, abort. This is not strictly required - the message # could be decrypted anyway. However, it would mean the sending device is not complying with the # specification, which is shady, thus it's not wrong to abort here either. device = next((device for device in devices if device.device_id == message.device_id), None) if device is None: raise SenderNotFound( "Couldn't find public information about the device which sent this message. I.e. the" " device either does not appear in the device list of the sending XMPP account, or the" " bundle of the sending device could not be downloaded." ) logging.getLogger(SessionManager.LOG_TAG).warning( "Sender device found by the manual device list update. Make sure your PEP subscription is set" " up correctly." ) # Check the trust level of the sending device. Abort in case of explicit distrust. if (await self._evaluate_custom_trust_level(device)) is TrustLevel.DISTRUSTED: raise SenderDistrusted( "The identity key corresponding to the sending device is explicitly distrusted." ) async def decrypt_key_material( backend: Backend, device: DeviceInformation, encrypted_key_material: EncryptedKeyMaterial ) -> Tuple[Session, PlainKeyMaterial]: """ Load an existing session and use it to decrypt some key material. Args: backend: The backend to load the session from. device: The device whose session to load. encrypted_key_material: The key material to decrypt. Returns: The session and the decrypted key material. Raises: NoSession: in case there is no session with the device in storage. DecryptionFailed: in case of backend-specific failures during decryption. Forwarded from :meth:`~omemo.backend.Backend.decrypt_key_material`. """ # If there is no key exchange, a session has to exist and should be loadable session = await backend.load_session(device.bare_jid, device.device_id) if session is None: raise NoSession( "There is no session with the sending device, and key exchange information required to" " build a new session is not included in the message." ) plain_key_material = await backend.decrypt_key_material(session, encrypted_key_material) return session, plain_key_material async def handle_key_exchange( backend: Backend, device: DeviceInformation, key_exchange: KeyExchange, encrypted_key_material: EncryptedKeyMaterial ) -> Tuple[Session, PlainKeyMaterial]: """ Handle a key exchange by building a new session if necessary, and decrypt some key material in the process. Args: backend: The backend to handle the key exchange with. device: The device which sent the key exchange information. key_exchange: The key exchange information. encrypted_key_material: The key material to decrypt. Returns: A session that was built using the key exchange information, either now or in the past, and the decrypted key material. Raises: PublicDataInconsistency: if the identity key that's part of the key exchange information doesn't match the identity key in the bundle of the device. KeyExchangeFailed: in case a new session needed to be built, and there was an error during the key exchange that's part of the session building. Forwarded from :meth:`~omemo.backend.Backend.build_session_passive`. DecryptionFailed: in case of backend-specific failures during decryption. Forwarded from the respective backend implementation. """ # Check whether the identity key matches the one we know if key_exchange.identity_key != device.identity_key: raise PublicDataInconsistency( "There is no session with the sending device. Key exchange information to build a new" " session is included in the message, however the identity key of the key exchange" " information does not match the identity key known for the sending device." ) # Check whether there is a session with the sending device already session = await backend.load_session(device.bare_jid, device.device_id) if session is not None: logging.getLogger(SessionManager.LOG_TAG).debug("Key exchange present, but session exists.") # If the key exchange would build a new session, treat this session as non-existent if ( session.initiation is Initiation.PASSIVE and session.key_exchange.builds_same_session(key_exchange) ): logging.getLogger(SessionManager.LOG_TAG).debug("Key exchange builds existing session.") else: logging.getLogger(SessionManager.LOG_TAG).warning( "Key exchange replaces existing session." ) session = None # If a new session needs to be built, do so. session, plain_key_material = await backend.build_session_passive( device.bare_jid, device.device_id, key_exchange, encrypted_key_material ) if session is None else (session, await backend.decrypt_key_material( session, encrypted_key_material )) # If an existing session is used, and the session was built through active session initiation, it # should now be flagged as confirmed. return session, plain_key_material # Inline if for type safety and pylint satisfaction. session, plain_key_material = ( await decrypt_key_material(backend, device, encrypted_key_material) if key_exchange is None else await handle_key_exchange(backend, device, key_exchange, encrypted_key_material) ) logging.getLogger(SessionManager.LOG_TAG).debug(f"Plain key material: {plain_key_material}") # Decrypt the message plaintext = None if message.content.empty else await backend.decrypt_plaintext( message.content, plain_key_material ) logging.getLogger(SessionManager.LOG_TAG).debug(f"Message decrypted: {plaintext}") # Persist the session following successful decryption await backend.store_session(session) # If this message was a key exchange, take care of pre key hiding/deletion. if key_exchange is not None: bundle_changed: bool if self.__synchronizing: # If the library is currently in history synchronization mode, hide the pre key but defer the # deletion. logging.getLogger(SessionManager.LOG_TAG).debug("Hiding pre key.") bundle_changed = await backend.hide_pre_key(session) else: # Otherwise, delete the pre key right away logging.getLogger(SessionManager.LOG_TAG).debug("Deleting pre key.") bundle_changed = await backend.delete_pre_key(session) if bundle_changed: num_visible_pre_keys = await backend.get_num_visible_pre_keys() if num_visible_pre_keys <= self.__pre_key_refill_threshold: logging.getLogger(SessionManager.LOG_TAG).debug("Refilling pre keys.") await backend.generate_pre_keys(100 - num_visible_pre_keys) bundle = await backend.get_bundle(self.__own_bare_jid, self.__own_device_id) await self._upload_bundle(bundle) # Send an empty message if necessary to avoid staleness and to "complete" the handshake in case this # was a key exchange if ( key_exchange is not None or (session.receiving_chain_length or 0) > self.__class__.STALENESS_MAGIC_NUMBER ): logging.getLogger(SessionManager.LOG_TAG).debug( "Sending/queueing empty message for session completion or staleness prevention." ) if self.__synchronizing: # Add this bare JID to the queue queued_jids = set((await storage.load_list(f"/queue/{session.namespace}", str)).maybe([])) queued_jids.add(session.bare_jid) await storage.store(f"/queue/{session.namespace}", list(queued_jids)) # Add this device id to the queue queued_device_ids = set((await storage.load_list( f"/queue/{session.namespace}/{session.bare_jid}", int )).maybe([])) queued_device_ids.add(session.device_id) await storage.store(f"/queue/{session.namespace}/{session.bare_jid}", list(queued_device_ids)) else: # If not in history synchronization mode, send the empty message right away await self.__send_empty_message(backend, session) logging.getLogger(SessionManager.LOG_TAG).debug("Post-decryption tasks completed.") # Return the plaintext and information about the sending device return (plaintext, device, plain_key_material) python-omemo-1.0.2/omemo/storage.py000066400000000000000000000315731433120400300172760ustar00rootroot00000000000000# This import from future (theoretically) enables sphinx_autodoc_typehints to handle type aliases better from __future__ import annotations # pylint: disable=unused-variable from abc import ABC, abstractmethod import base64 import copy from typing import Any, Callable, Dict, Generic, List, Optional, Type, TypeVar, Union, cast from .types import JSONType, OMEMOException __all__ = [ # pylint: disable=unused-variable "Just", "Maybe", "Nothing", "NothingException", "Storage", "StorageException" ] class StorageException(OMEMOException): """ Parent type for all exceptions specifically raised by methods of :class:`Storage`. """ ValueTypeT = TypeVar("ValueTypeT") DefaultTypeT = TypeVar("DefaultTypeT") MappedValueTypeT = TypeVar("MappedValueTypeT") class Maybe(ABC, Generic[ValueTypeT]): """ typing's `Optional[A]` is just an alias for `Union[None, A]`, which means if `A` is a union itself that allows `None`, the `Optional[A]` doesn't add anything. E.g. `Optional[Optional[X]] = Optional[X]` is true for any type `X`. This Maybe class actually differenciates whether a value is set or not. All incoming and outgoing values or cloned using :func:`copy.deepcopy`, such that values stored in a Maybe instance are not affected by outside application logic. """ @property @abstractmethod def is_just(self) -> bool: """ Returns: Whether this is a :class:`Just`. """ @property @abstractmethod def is_nothing(self) -> bool: """ Returns: Whether this is a :class:`Nothing`. """ @abstractmethod def from_just(self) -> ValueTypeT: """ Returns: The value if this is a :class:`Just`. Raises: NothingException: if this is a :class:`Nothing`. """ @abstractmethod def maybe(self, default: DefaultTypeT) -> Union[ValueTypeT, DefaultTypeT]: """ Args: default: The value to return if this is in instance of :class:`Nothing`. Returns: The value if this is a :class:`Just`, or the default value if this is a :class:`Nothing`. The default is returned by reference in that case. """ @abstractmethod def fmap(self, function: Callable[[ValueTypeT], MappedValueTypeT]) -> "Maybe[MappedValueTypeT]": """ Apply a mapping function. Args: function: The mapping function. Returns: A new :class:`Just` containing the mapped value if this is a :class:`Just`. A new :class:`Nothing` if this is a :class:`Nothing`. """ class NothingException(Exception): """ Raised by :meth:`Maybe.from_just`, in case the :class:`Maybe` is a :class:`Nothing`. """ class Nothing(Maybe[ValueTypeT]): """ A :class:`Maybe` that does not hold a value. """ def __init__(self) -> None: """ Initialize a :class:`Nothing`, representing an empty :class:`Maybe`. """ @property def is_just(self) -> bool: return False @property def is_nothing(self) -> bool: return True def from_just(self) -> ValueTypeT: raise NothingException("Maybe.fromJust: Nothing") # -- yuck def maybe(self, default: DefaultTypeT) -> DefaultTypeT: return default def fmap(self, function: Callable[[ValueTypeT], MappedValueTypeT]) -> "Nothing[MappedValueTypeT]": return Nothing() class Just(Maybe[ValueTypeT]): """ A :class:`Maybe` that does hold a value. """ def __init__(self, value: ValueTypeT) -> None: """ Initialize a :class:`Just`, representing a :class:`Maybe` that holds a value. Args: value: The value to store in this :class:`Just`. """ self.__value = copy.deepcopy(value) @property def is_just(self) -> bool: return True @property def is_nothing(self) -> bool: return False def from_just(self) -> ValueTypeT: return copy.deepcopy(self.__value) def maybe(self, default: DefaultTypeT) -> ValueTypeT: return copy.deepcopy(self.__value) def fmap(self, function: Callable[[ValueTypeT], MappedValueTypeT]) -> "Just[MappedValueTypeT]": return Just(function(copy.deepcopy(self.__value))) PrimitiveTypeT = TypeVar("PrimitiveTypeT", None, float, int, str, bool) class Storage(ABC): """ A simple key/value storage class with optional caching (on by default). Keys can be any Python string, values any JSON-serializable structure. Warning: Writing (and deletion) operations must be performed right away, before returning from the method. Such operations must not be cached or otherwise deferred. Warning: All parameters must be treated as immutable unless explicitly noted otherwise. Note: The :class:`Maybe` type performs the additional job of cloning stored and returned values, which essential to decouple the cached values from the application logic. """ def __init__(self, disable_cache: bool = False): """ Configure caching behaviour of the storage. Args: disable_cache: Whether to disable the cache, which is on by default. Use this parameter if your storage implementation handles caching itself, to avoid pointless double caching. """ self.__cache: Optional[Dict[str, Maybe[JSONType]]] = None if disable_cache else {} @abstractmethod async def _load(self, key: str) -> Maybe[JSONType]: """ Load a value. Args: key: The key identifying the value. Returns: The loaded value, if it exists. Raises: StorageException: if any kind of storage operation failed. Feel free to raise a subclass instead. """ @abstractmethod async def _store(self, key: str, value: JSONType) -> Any: """ Store a value. Args: key: The key identifying the value. value: The value to store under the given key. Returns: Anything, the return value is ignored. Raises: StorageException: if any kind of storage operation failed. Feel free to raise a subclass instead. """ @abstractmethod async def _delete(self, key: str) -> Any: """ Delete a value, if it exists. Args: key: The key identifying the value to delete. Returns: Anything, the return value is ignored. Raises: StorageException: if any kind of storage operation failed. Feel free to raise a subclass instead. Do not raise if the key doesn't exist. """ async def load(self, key: str) -> Maybe[JSONType]: """ Load a value. Args: key: The key identifying the value. Returns: The loaded value, if it exists. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_load`. """ if self.__cache is not None and key in self.__cache: return self.__cache[key] value = await self._load(key) if self.__cache is not None: self.__cache[key] = value return value async def store(self, key: str, value: JSONType) -> None: """ Store a value. Args: key: The key identifying the value. value: The value to store under the given key. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_store`. """ await self._store(key, value) if self.__cache is not None: self.__cache[key] = Just(value) async def delete(self, key: str) -> None: """ Delete a value, if it exists. Args: key: The key identifying the value to delete. Raises: StorageException: if any kind of storage operation failed. Does not raise if the key doesn't exist. Forwarded from :meth:`_delete`. """ await self._delete(key) if self.__cache is not None: self.__cache[key] = Nothing() async def store_bytes(self, key: str, value: bytes) -> None: """ Variation of :meth:`store` for storing specifically bytes values. Args: key: The key identifying the value. value: The value to store under the given key. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_store`. """ await self.store(key, base64.urlsafe_b64encode(value).decode("ASCII")) async def load_primitive(self, key: str, primitive: Type[PrimitiveTypeT]) -> Maybe[PrimitiveTypeT]: """ Variation of :meth:`load` for loading specifically primitive values. Args: key: The key identifying the value. primitive: The primitive type of the value. Returns: The loaded and type-checked value, if it exists. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_load`. """ def check_type(value: JSONType) -> PrimitiveTypeT: if isinstance(value, primitive): return value raise TypeError(f"The value stored for key {key} is not a {primitive}: {value}") return (await self.load(key)).fmap(check_type) async def load_bytes(self, key: str) -> Maybe[bytes]: """ Variation of :meth:`load` for loading specifically bytes values. Args: key: The key identifying the value. Returns: The loaded and type-checked value, if it exists. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_load`. """ def check_type(value: JSONType) -> bytes: if isinstance(value, str): return base64.urlsafe_b64decode(value.encode("ASCII")) raise TypeError(f"The value stored for key {key} is not a str/bytes: {value}") return (await self.load(key)).fmap(check_type) async def load_optional( self, key: str, primitive: Type[PrimitiveTypeT] ) -> Maybe[Optional[PrimitiveTypeT]]: """ Variation of :meth:`load` for loading specifically optional primitive values. Args: key: The key identifying the value. primitive: The primitive type of the optional value. Returns: The loaded and type-checked value, if it exists. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_load`. """ def check_type(value: JSONType) -> Optional[PrimitiveTypeT]: if value is None or isinstance(value, primitive): return value raise TypeError(f"The value stored for key {key} is not an optional {primitive}: {value}") return (await self.load(key)).fmap(check_type) async def load_list(self, key: str, primitive: Type[PrimitiveTypeT]) -> Maybe[List[PrimitiveTypeT]]: """ Variation of :meth:`load` for loading specifically lists of primitive values. Args: key: The key identifying the value. primitive: The primitive type of the list elements. Returns: The loaded and type-checked value, if it exists. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_load`. """ def check_type(value: JSONType) -> List[PrimitiveTypeT]: if isinstance(value, list) and all(isinstance(element, primitive) for element in value): return cast(List[PrimitiveTypeT], value) raise TypeError(f"The value stored for key {key} is not a list of {primitive}: {value}") return (await self.load(key)).fmap(check_type) async def load_dict( self, key: str, primitive: Type[PrimitiveTypeT] ) -> Maybe[Dict[str, PrimitiveTypeT]]: """ Variation of :meth:`load` for loading specifically dictionaries of primitive values. Args: key: The key identifying the value. primitive: The primitive type of the dictionary values. Returns: The loaded and type-checked value, if it exists. Raises: StorageException: if any kind of storage operation failed. Forwarded from :meth:`_load`. """ def check_type(value: JSONType) -> Dict[str, PrimitiveTypeT]: if isinstance(value, dict) and all(isinstance(v, primitive) for v in value.values()): return cast(Dict[str, PrimitiveTypeT], value) raise TypeError(f"The value stored for key {key} is not a dict of {primitive}: {value}") return (await self.load(key)).fmap(check_type) python-omemo-1.0.2/omemo/types.py000066400000000000000000000040451433120400300167700ustar00rootroot00000000000000# This import from future (theoretically) enables sphinx_autodoc_typehints to handle type aliases better from __future__ import annotations # pylint: disable=unused-variable import enum from typing import FrozenSet, List, Mapping, NamedTuple, Optional, Tuple, Union __all__ = [ # pylint: disable=unused-variable "AsyncFramework", "DeviceInformation", "JSONType", "OMEMOException", "TrustLevel" ] @enum.unique class AsyncFramework(enum.Enum): """ Frameworks for asynchronous code supported by python-omemo. """ ASYNCIO: str = "ASYNCIO" TWISTED: str = "TWISTED" class OMEMOException(Exception): """ Parent type for all custom exceptions in this library. """ class DeviceInformation(NamedTuple): # pylint: disable=invalid-name """ Structure containing information about a single OMEMO device. """ namespaces: FrozenSet[str] active: FrozenSet[Tuple[str, bool]] bare_jid: str device_id: int identity_key: bytes trust_level_name: str label: Optional[str] @enum.unique class TrustLevel(enum.Enum): """ The three core trust levels. """ TRUSTED: str = "TRUSTED" DISTRUSTED: str = "DISTRUSTED" UNDECIDED: str = "UNDECIDED" # # Thanks @vanburgerberg - https://github.com/python/typing/issues/182 # if TYPE_CHECKING: # class JSONArray(list[JSONType], Protocol): # type: ignore # __class__: Type[list[JSONType]] # type: ignore # # class JSONObject(dict[str, JSONType], Protocol): # type: ignore # __class__: Type[dict[str, JSONType]] # type: ignore # # JSONType = Union[None, float, int, str, bool, JSONArray, JSONObject] # Sadly @vanburgerberg's solution doesn't seem to like Dict[str, bool], thus for now an incomplete JSON # type with finite levels of depth. Primitives = Union[None, float, int, str, bool] JSONType2 = Union[Primitives, List[Primitives], Mapping[str, Primitives]] JSONType1 = Union[Primitives, List[JSONType2], Mapping[str, JSONType2]] JSONType = Union[Primitives, List[JSONType1], Mapping[str, JSONType1]] python-omemo-1.0.2/omemo/version.py000066400000000000000000000003231433120400300173040ustar00rootroot00000000000000__all__ = [ "__version__" ] # pylint: disable=unused-variable __version__ = {} __version__["short"] = "1.0.2" __version__["tag"] = "stable" __version__["full"] = f"{__version__['short']}-{__version__['tag']}" python-omemo-1.0.2/pylintrc000066400000000000000000000362471433120400300157360ustar00rootroot00000000000000[MASTER] # A comma-separated list of package or module names from where C extensions may # be loaded. Extensions are loading into the active Python interpreter and may # run arbitrary code. extension-pkg-whitelist= # Add files or directories to the blacklist. They should be base names, not # paths. ignore=CVS # Add files or directories matching the regex patterns to the blacklist. The # regex matches against base names, not paths. ignore-patterns= # Python code to execute, usually for sys.path manipulation such as # pygtk.require(). #init-hook= # Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the # number of processors available to use. jobs=1 # Control the amount of potential inferred values when inferring a single # object. This can help the performance when dealing with large functions or # complex, nested conditions. limit-inference-results=100 # List of plugins (as comma separated values of python module names) to load, # usually to register additional checkers. load-plugins= # Pickle collected data for later comparisons. persistent=yes # Specify a configuration file. #rcfile= # When enabled, pylint would attempt to guess common misconfiguration and emit # user-friendly hints instead of false-positive error messages. suggestion-mode=yes # Allow loading of arbitrary C extensions. Extensions are imported into the # active Python interpreter and may run arbitrary code. unsafe-load-any-extension=no [MESSAGES CONTROL] # Only show warnings with the listed confidence levels. Leave empty to show # all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. confidence= # Disable the message, report, category or checker with the given id(s). You # can either give multiple identifiers separated by comma (,) or put this # option multiple times (only on the command line, not in the configuration # file where it should appear only once). You can also use "--disable=all" to # disable everything first and then reenable specific checks. For example, if # you want to run only the similarities checker, you can use "--disable=all # --enable=similarities". If you want to run only the classes checker, but have # no Warning level messages displayed, use "--disable=all --enable=classes # --disable=W". disable=missing-module-docstring, duplicate-code, fixme, logging-fstring-interpolation # Enable the message, report, category or checker with the given id(s). You can # either give multiple identifier separated by comma (,) or put this option # multiple time (only on the command line, not in the configuration file where # it should appear only once). See also the "--disable" option for examples. enable=useless-suppression [REPORTS] # Python expression which should return a score less than or equal to 10. You # have access to the variables 'error', 'warning', 'refactor', and 'convention' # which contain the number of messages in each category, as well as 'statement' # which is the total number of statements analyzed. This score is used by the # global evaluation report (RP0004). evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) # Template used to display messages. This is a python new-style format string # used to format the message information. See doc for all details. #msg-template= # Set the output format. Available formats are text, parseable, colorized, json # and msvs (visual studio). You can also give a reporter class, e.g. # mypackage.mymodule.MyReporterClass. output-format=text # Tells whether to display a full report or only the messages. reports=no # Activate the evaluation score. score=yes [REFACTORING] # Maximum number of nested blocks for function / method body max-nested-blocks=5 # Complete name of functions that never returns. When checking for # inconsistent-return-statements if a never returning function is called then # it will be considered as an explicit return statement and no message will be # printed. never-returning-functions=sys.exit [SPELLING] # Limits count of emitted suggestions for spelling mistakes. max-spelling-suggestions=4 # Spelling dictionary name. Available dictionaries: none. To make it work, # install the python-enchant package. spelling-dict= # List of comma separated words that should not be checked. spelling-ignore-words= # A path to a file that contains the private dictionary; one word per line. spelling-private-dict-file= # Tells whether to store unknown words to the private dictionary (see the # --spelling-private-dict-file option) instead of raising a message. spelling-store-unknown-words=no [VARIABLES] # List of additional names supposed to be defined in builtins. Remember that # you should avoid defining new builtins when possible. additional-builtins= # Tells whether unused global variables should be treated as a violation. allow-global-unused-variables=no # List of strings which can identify a callback function by name. A callback # name must start or end with one of those strings. callbacks=cb_, _cb # A regular expression matching the name of dummy variables (i.e. expected to # not be used). dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ # Argument names that match this expression will be ignored. Default to name # with leading underscore. ignored-argument-names=_.*|^ignored_|^unused_ # Tells whether we should check for unused import in __init__ files. init-import=no # List of qualified module names which can have objects that can redefine # builtins. redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io [LOGGING] # Format style used to check logging format string. `old` means using % # formatting, `new` is for `{}` formatting,and `fstr` is for f-strings. logging-format-style=old # Logging modules to check that the string format arguments are in logging # function parameter format. logging-modules=logging [FORMAT] # Expected format of line ending, e.g. empty (any line ending), LF or CRLF. expected-line-ending-format= # Regexp for a line that is allowed to be longer than the limit. ignore-long-lines=^\s*(# )??$ # Number of spaces of indent required inside a hanging or continued line. indent-after-paren=4 # String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 # tab). indent-string=' ' # Maximum number of characters on a single line. max-line-length=110 # Maximum number of lines in a module. max-module-lines=10000 # Allow the body of a class to be on the same line as the declaration if body # contains single statement. single-line-class-stmt=no # Allow the body of an if to be on the same line as the test if there is no # else. single-line-if-stmt=yes [TYPECHECK] # List of decorators that produce context managers, such as # contextlib.contextmanager. Add to this list to register other decorators that # produce valid context managers. contextmanager-decorators=contextlib.contextmanager # List of members which are set dynamically and missed by pylint inference # system, and so shouldn't trigger E1101 when accessed. Python regular # expressions are accepted. generated-members= # Tells whether missing members accessed in mixin class should be ignored. A # mixin class is detected if its name ends with "mixin" (case insensitive). ignore-mixin-members=yes # Tells whether to warn about missing members when the owner of the attribute # is inferred to be None. ignore-none=no # This flag controls whether pylint should warn about no-member and similar # checks whenever an opaque object is returned when inferring. The inference # can return multiple potential results while evaluating a Python object, but # some branches might not be evaluated, which results in partial inference. In # that case, it might be useful to still emit no-member and other checks for # the rest of the inferred objects. ignore-on-opaque-inference=no # List of class names for which member attributes should not be checked (useful # for classes with dynamically set attributes). This supports the use of # qualified names. ignored-classes=optparse.Values,thread._local,_thread._local # List of module names for which member attributes should not be checked # (useful for modules/projects where namespaces are manipulated during runtime # and thus existing member attributes cannot be deduced by static analysis). It # supports qualified module names, as well as Unix pattern matching. ignored-modules= # Show a hint with possible names when a member name was not found. The aspect # of finding the hint is based on edit distance. missing-member-hint=yes # The minimum edit distance a name should have in order to be considered a # similar match for a missing member name. missing-member-hint-distance=1 # The total number of similar names that should be taken in consideration when # showing a hint for a missing member. missing-member-max-choices=1 # List of decorators that change the signature of a decorated function. signature-mutators= [STRING] # This flag controls whether the implicit-str-concat-in-sequence should # generate a warning on implicit string concatenation in sequences defined over # several lines. check-str-concat-over-line-jumps=no [SIMILARITIES] # Ignore comments when computing similarities. ignore-comments=yes # Ignore docstrings when computing similarities. ignore-docstrings=yes # Ignore imports when computing similarities. ignore-imports=no # Minimum lines number of a similarity. min-similarity-lines=4 [BASIC] # Naming style matching correct argument names. argument-naming-style=snake_case # Regular expression matching correct argument names. Overrides argument- # naming-style. #argument-rgx= # Naming style matching correct attribute names. attr-naming-style=snake_case # Regular expression matching correct attribute names. Overrides attr-naming- # style. #attr-rgx= # Bad variable names which should always be refused, separated by a comma. bad-names=foo, bar, baz, toto, tutu, tata # Naming style matching correct class attribute names. class-attribute-naming-style=UPPER_CASE # Regular expression matching correct class attribute names. Overrides class- # attribute-naming-style. #class-attribute-rgx= # Naming style matching correct class names. class-naming-style=PascalCase # Regular expression matching correct class names. Overrides class-naming- # style. #class-rgx= # Naming style matching correct constant names. const-naming-style=any # Regular expression matching correct constant names. Overrides const-naming- # style. #const-rgx= # Minimum line length for functions/classes that require docstrings, shorter # ones are exempt. docstring-min-length=-1 # Naming style matching correct function names. function-naming-style=snake_case # Regular expression matching correct function names. Overrides function- # naming-style. #function-rgx= # Good variable names which should always be accepted, separated by a comma. good-names=i, j, e, # exceptions in except blocks _ # Include a hint for the correct naming format with invalid-name. include-naming-hint=no # Naming style matching correct inline iteration names. inlinevar-naming-style=any # Regular expression matching correct inline iteration names. Overrides # inlinevar-naming-style. #inlinevar-rgx= # Naming style matching correct method names. method-naming-style=snake_case # Regular expression matching correct method names. Overrides method-naming- # style. #method-rgx= # Naming style matching correct module names. module-naming-style=snake_case # Regular expression matching correct module names. Overrides module-naming- # style. #module-rgx= # Colon-delimited sets of names that determine each other's naming style when # the name regexes allow several styles. name-group= # Regular expression which should only match function or class names that do # not require a docstring. no-docstring-rgx=^_ # List of decorators that produce properties, such as abc.abstractproperty. Add # to this list to register other decorators that produce valid properties. # These decorators are taken in consideration only for invalid-name. property-classes=abc.abstractproperty # Naming style matching correct variable names. variable-naming-style=snake_case # Regular expression matching correct variable names. Overrides variable- # naming-style. #variable-rgx= [MISCELLANEOUS] # List of note tags to take in consideration, separated by a comma. notes=FIXME, XXX, TODO [IMPORTS] # List of modules that can be imported at any level, not just the top level # one. allow-any-import-level= # Allow wildcard imports from modules that define __all__. allow-wildcard-with-all=no # Analyse import fallback blocks. This can be used to support both Python 2 and # 3 compatible code, which means that the block might have code that exists # only in one or another interpreter, leading to false positives when analysed. analyse-fallback-blocks=yes # Deprecated modules which should not be used, separated by a comma. deprecated-modules=optparse,tkinter.tix # Create a graph of external dependencies in the given file (report RP0402 must # not be disabled). ext-import-graph= # Create a graph of every (i.e. internal and external) dependencies in the # given file (report RP0402 must not be disabled). import-graph= # Create a graph of internal dependencies in the given file (report RP0402 must # not be disabled). int-import-graph= # Force import order to recognize a module as part of the standard # compatibility libraries. known-standard-library= # Force import order to recognize a module as part of a third party library. known-third-party=enchant # Couples of modules and preferred modules, separated by a comma. preferred-modules= [CLASSES] # List of method names used to declare (i.e. assign) instance attributes. defining-attr-methods=__init__, __new__, setUp, __post_init__, create # List of member names, which should be excluded from the protected access # warning. exclude-protected=_asdict, _fields, _replace, _source, _make # List of valid names for the first argument in a class method. valid-classmethod-first-arg=cls # List of valid names for the first argument in a metaclass class method. valid-metaclass-classmethod-first-arg=cls [DESIGN] # Maximum number of arguments for function / method. max-args=100 # Maximum number of attributes for a class (see R0902). max-attributes=100 # Maximum number of boolean expressions in an if statement (see R0916). max-bool-expr=10 # Maximum number of branch for function / method body. max-branches=100 # Maximum number of locals for function / method body. max-locals=100 # Maximum number of parents for a class (see R0901). max-parents=10 # Maximum number of public methods for a class (see R0904). max-public-methods=100 # Maximum number of return / yield for function / method body. max-returns=100 # Maximum number of statements in function / method body. max-statements=1000 # Minimum number of public methods for a class (see R0903). min-public-methods=0 [EXCEPTIONS] # Exceptions that will emit a warning when being caught. Defaults to # "BaseException, Exception". overgeneral-exceptions=BaseException, Exception python-omemo-1.0.2/requirements.txt000066400000000000000000000000521433120400300174140ustar00rootroot00000000000000XEdDSA>=1.0.0,<2 typing-extensions>=4.3.0 python-omemo-1.0.2/setup.py000066400000000000000000000040001433120400300156370ustar00rootroot00000000000000# pylint: disable=exec-used import os from typing import Dict, Union, List from setuptools import setup, find_packages # type: ignore[import] source_root = os.path.join(os.path.dirname(os.path.abspath(__file__)), "omemo") version_scope: Dict[str, Dict[str, str]] = {} with open(os.path.join(source_root, "version.py"), encoding="utf-8") as f: exec(f.read(), version_scope) version = version_scope["__version__"] project_scope: Dict[str, Dict[str, Union[str, List[str]]]] = {} with open(os.path.join(source_root, "project.py"), encoding="utf-8") as f: exec(f.read(), project_scope) project = project_scope["project"] with open("README.md", encoding="utf-8") as f: long_description = f.read() classifiers = [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy" ] classifiers.extend(project["categories"]) if version["tag"] == "alpha": classifiers.append("Development Status :: 3 - Alpha") if version["tag"] == "beta": classifiers.append("Development Status :: 4 - Beta") if version["tag"] == "stable": classifiers.append("Development Status :: 5 - Production/Stable") del project["categories"] del project["year"] setup( version=version["short"], long_description=long_description, long_description_content_type="text/markdown", license="MIT", packages=find_packages(exclude=["tests"]), install_requires=[ "XEdDSA>=1.0.0,<2", "typing-extensions>=4.3.0" ], python_requires=">=3.7", include_package_data=True, zip_safe=False, classifiers=classifiers, **project ) python-omemo-1.0.2/tests/000077500000000000000000000000001433120400300152755ustar00rootroot00000000000000python-omemo-1.0.2/tests/LICENSE000066400000000000000000001033331433120400300163050ustar00rootroot00000000000000 GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software. A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public. The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version. An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU Affero General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Remote Network Interaction; Use with the GNU General Public License. Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU General Public License that is incorporated pursuant to the following paragraph. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU General Public License. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If your software can interact with users remotely through a computer network, you should also make sure that it provides a way for users to get its source. For example, if your program is a web application, its interface could display a "Source" link that leads users to an archive of the code. There are many ways you could offer source, and different solutions will be better for different programs; see section 13 for the specific requirements. You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU AGPL, see . python-omemo-1.0.2/tests/__init__.py000066400000000000000000000000401433120400300174000ustar00rootroot00000000000000# To make relative imports work python-omemo-1.0.2/tests/test_session_manager.py000066400000000000000000000266331433120400300220750ustar00rootroot00000000000000import enum from typing import Dict, FrozenSet, List, NamedTuple, Optional, Tuple, Type import xml.etree.ElementTree as ET import oldmemo import oldmemo.etree import twomemo import twomemo.etree import pytest from typing_extensions import assert_never import omemo __all__ = [ # pylint: disable=unused-variable "test_regression0" ] pytestmark = pytest.mark.asyncio # pylint: disable=unused-variable @enum.unique class TrustLevel(enum.Enum): """ Trust levels modeling simple manual trust. """ TRUSTED: str = "TRUSTED" UNDECIDED: str = "UNDECIDED" DISTRUSTED: str = "DISTRUSTED" class InMemoryStorage(omemo.Storage): """ Volatile storage implementation with the values held in memory. """ def __init__(self) -> None: super().__init__(True) self.__storage: Dict[str, omemo.JSONType] = {} async def _load(self, key: str) -> omemo.Maybe[omemo.JSONType]: try: return omemo.Just(self.__storage[key]) except KeyError: return omemo.Nothing() async def _store(self, key: str, value: omemo.JSONType) -> None: self.__storage[key] = value async def _delete(self, key: str) -> None: self.__storage.pop(key, None) class BundleStorageKey(NamedTuple): # pylint: disable=invalid-name """ The key identifying a bundle in the tests. """ namespace: str bare_jid: str device_id: int class DeviceListStorageKey(NamedTuple): # pylint: disable=invalid-name """ The key identifying a device list in the tests. """ namespace: str bare_jid: str BundleStorage = Dict[BundleStorageKey, omemo.Bundle] DeviceListStorage = Dict[DeviceListStorageKey, Dict[int, Optional[str]]] MessageQueue = List[Tuple[str, omemo.Message]] def make_session_manager_impl( own_bare_jid: str, bundle_storage: BundleStorage, device_list_storage: DeviceListStorage, message_queue: MessageQueue ) -> Type[omemo.SessionManager]: """ Args: own_bare_jid: The bare JID of the account that will be used with the session manager instances created from this implementation. bundle_storage: The dictionary to "upload", "download" and delete bundles to/from. device_list_storage: The dictionary to "upload" and "download" device lists to/from. message_queue: The list to "send" automated messages to. The first entry of each tuple is the bare JID of the recipient. The second entry is the message itself. Returns: A session manager implementation which sends/uploads/downloads/deletes data to/from the collections given as parameters. """ class SessionManagerImpl(omemo.SessionManager): # pylint: disable=missing-class-docstring @staticmethod async def _upload_bundle(bundle: omemo.Bundle) -> None: bundle_storage[BundleStorageKey( namespace=bundle.namespace, bare_jid=bundle.bare_jid, device_id=bundle.device_id )] = bundle @staticmethod async def _download_bundle(namespace: str, bare_jid: str, device_id: int) -> omemo.Bundle: try: return bundle_storage[BundleStorageKey( namespace=namespace, bare_jid=bare_jid, device_id=device_id )] except KeyError as e: raise omemo.BundleDownloadFailed() from e @staticmethod async def _delete_bundle(namespace: str, device_id: int) -> None: try: bundle_storage.pop(BundleStorageKey( namespace=namespace, bare_jid=own_bare_jid, device_id=device_id )) except KeyError as e: raise omemo.BundleDeletionFailed() from e @staticmethod async def _upload_device_list(namespace: str, device_list: Dict[int, Optional[str]]) -> None: device_list_storage[DeviceListStorageKey( namespace=namespace, bare_jid=own_bare_jid )] = device_list @staticmethod async def _download_device_list(namespace: str, bare_jid: str) -> Dict[int, Optional[str]]: try: return device_list_storage[DeviceListStorageKey( namespace=namespace, bare_jid=bare_jid )] except KeyError: return {} async def _evaluate_custom_trust_level(self, device: omemo.DeviceInformation) -> omemo.TrustLevel: try: trust_level = TrustLevel(device.trust_level_name) except ValueError as e: raise omemo.UnknownTrustLevel() from e if trust_level is TrustLevel.TRUSTED: return omemo.TrustLevel.TRUSTED if trust_level is TrustLevel.UNDECIDED: return omemo.TrustLevel.UNDECIDED if trust_level is TrustLevel.DISTRUSTED: return omemo.TrustLevel.DISTRUSTED assert_never(trust_level) async def _make_trust_decision( self, undecided: FrozenSet[omemo.DeviceInformation], identifier: Optional[str] ) -> None: for device in undecided: await self.set_trust(device.bare_jid, device.identity_key, TrustLevel.TRUSTED.name) @staticmethod async def _send_message(message: omemo.Message, bare_jid: str) -> None: message_queue.append((bare_jid, message)) return SessionManagerImpl NS_TWOMEMO = twomemo.twomemo.NAMESPACE NS_OLDMEMO = oldmemo.oldmemo.NAMESPACE ALICE_BARE_JID = "alice@example.org" BOB_BARE_JID = "bob@example.org" async def test_regression0() -> None: """ Test a specific scenario that caused trouble during Libervia's JET plugin implementation. """ bundle_storage: BundleStorage = {} device_list_storage: DeviceListStorage = {} alice_message_queue: MessageQueue = [] bob_message_queue: MessageQueue = [] AliceSessionManagerImpl = make_session_manager_impl( ALICE_BARE_JID, bundle_storage, device_list_storage, alice_message_queue ) BobSessionManagerImpl = make_session_manager_impl( BOB_BARE_JID, bundle_storage, device_list_storage, bob_message_queue ) alice_storage = InMemoryStorage() bob_storage = InMemoryStorage() # Create a session manager for each party alice_session_manager = await AliceSessionManagerImpl.create( backends=[ twomemo.Twomemo(alice_storage), oldmemo.Oldmemo(alice_storage) ], storage=alice_storage, own_bare_jid=ALICE_BARE_JID, initial_own_label=None, undecided_trust_level_name=TrustLevel.UNDECIDED.name ) bob_session_manager = await BobSessionManagerImpl.create( backends=[ twomemo.Twomemo(bob_storage), oldmemo.Oldmemo(bob_storage) ], storage=bob_storage, own_bare_jid=BOB_BARE_JID, initial_own_label=None, undecided_trust_level_name=TrustLevel.UNDECIDED.name ) # Exit history synchronization mode await alice_session_manager.after_history_sync() await bob_session_manager.after_history_sync() # Ask both parties to refresh the device lists of the other party await alice_session_manager.refresh_device_list(NS_TWOMEMO, BOB_BARE_JID) await alice_session_manager.refresh_device_list(NS_OLDMEMO, BOB_BARE_JID) await bob_session_manager.refresh_device_list(NS_TWOMEMO, ALICE_BARE_JID) await bob_session_manager.refresh_device_list(NS_OLDMEMO, ALICE_BARE_JID) # Have Alice encrypt an initial message to Bob to set up sessions between them for namespace in [ NS_TWOMEMO, NS_OLDMEMO ]: messages, encryption_errors = await alice_session_manager.encrypt( bare_jids=frozenset({ BOB_BARE_JID }), plaintext={ namespace: b"Hello, Bob!" }, backend_priority_order=[ namespace ] ) assert len(messages) == 1 assert len(encryption_errors) == 0 # Have Bob decrypt the message message = next(iter(messages.keys())) plaintext, _, _ = await bob_session_manager.decrypt(message) assert plaintext == b"Hello, Bob!" # Bob should now have an empty message for Alice in his message queue assert len(bob_message_queue) == 1 bob_queued_message_recipient, bob_queued_message = bob_message_queue.pop() assert bob_queued_message_recipient == ALICE_BARE_JID # Have Alice decrypt the empty message to complete the session intiation plaintext, _, _ = await alice_session_manager.decrypt(bob_queued_message) assert plaintext is None # The part that caused trouble in the Libervia JET plugin implementation was an attempt at sending an # oldmemo KeyTransportElement. 32 zero-bytes were used as the plaintext, and the payload was manually # removed from the serialized XML. This test tests that scenario with both twomemo and oldmemo. for namespace in [ NS_TWOMEMO, NS_OLDMEMO ]: messages, encryption_errors = await alice_session_manager.encrypt( bare_jids=frozenset({ BOB_BARE_JID }), plaintext={ namespace: b"\x00" * 32 }, backend_priority_order=[ namespace ] ) assert len(messages) == 1 assert len(encryption_errors) == 0 message = next(iter(messages.keys())) # Serialize the message to XML, remove the payload, and parse it again encrypted_elt: Optional[ET.Element] = None if namespace == NS_TWOMEMO: encrypted_elt = twomemo.etree.serialize_message(message) if namespace == NS_OLDMEMO: encrypted_elt = oldmemo.etree.serialize_message(message) assert encrypted_elt is not None for payload_elt in encrypted_elt.findall(f"{{{namespace}}}payload"): encrypted_elt.remove(payload_elt) if namespace == NS_TWOMEMO: message = twomemo.etree.parse_message(encrypted_elt, ALICE_BARE_JID) if namespace == NS_OLDMEMO: message = await oldmemo.etree.parse_message( encrypted_elt, ALICE_BARE_JID, BOB_BARE_JID, bob_session_manager ) # Decrypt the message on Bob's side plaintext, _, _ = await bob_session_manager.decrypt(message) assert plaintext is None # At this point, communication between both parties was broken. Try to send two messages back and forth. for namespace in [ NS_TWOMEMO, NS_OLDMEMO ]: messages, encryption_errors = await alice_session_manager.encrypt( bare_jids=frozenset({ BOB_BARE_JID }), plaintext={ namespace: b"Hello again, Bob!" }, backend_priority_order=[ namespace ] ) assert len(messages) == 1 assert len(encryption_errors) == 0 plaintext, _, _ = await bob_session_manager.decrypt(next(iter(messages.keys()))) assert plaintext == b"Hello again, Bob!" messages, encryption_errors = await bob_session_manager.encrypt( bare_jids=frozenset({ ALICE_BARE_JID }), plaintext={ namespace: b"Hello back, Alice!" }, backend_priority_order=[ namespace ] ) assert len(messages) == 1 assert len(encryption_errors) == 0 plaintext, _, _ = await alice_session_manager.decrypt(next(iter(messages.keys()))) assert plaintext == b"Hello back, Alice!"