File: crypto.py

package info (click to toggle)
mautrix-python 0.20.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,812 kB
  • sloc: python: 19,103; makefile: 16
file content (90 lines) | stat: -rw-r--r-- 2,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Copyright (c) 2022 Tulir Asokan
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import annotations

import warnings

from mautrix.types import IdentityKey, SessionID

from .base import MatrixError


class CryptoError(MatrixError):
    def __init__(self, message: str) -> None:
        super().__init__(message)
        self.message = message


class EncryptionError(CryptoError):
    pass


class SessionShareError(CryptoError):
    pass


class DecryptionError(CryptoError):
    @property
    def human_message(self) -> str:
        return "the bridge failed to decrypt the message"


class MatchingSessionDecryptionError(DecryptionError):
    pass


class GroupSessionWithheldError(DecryptionError):
    def __init__(self, session_id: SessionID, withheld_code: str) -> None:
        super().__init__(f"Session ID {session_id} was withheld ({withheld_code})")
        self.withheld_code = withheld_code


class SessionNotFound(DecryptionError):
    def __init__(self, session_id: SessionID, sender_key: IdentityKey | None = None) -> None:
        super().__init__(
            f"Failed to decrypt megolm event: no session with given ID {session_id} found"
        )
        self.session_id = session_id
        self._sender_key = sender_key

    @property
    def human_message(self) -> str:
        return "the bridge hasn't received the decryption keys"

    @property
    def sender_key(self) -> IdentityKey | None:
        """
        .. deprecated:: 0.17.0
            Matrix v1.3 deprecated the device_id and sender_key fields in megolm events.
        """
        warnings.warn(
            "The sender_key field in Megolm events was deprecated in Matrix 1.3",
            DeprecationWarning,
        )
        return self._sender_key


class DuplicateMessageIndex(DecryptionError):
    def __init__(self) -> None:
        super().__init__("Duplicate message index")


class VerificationError(DecryptionError):
    def __init__(self) -> None:
        super().__init__("Device keys in session and cached device info do not match")


class DecryptedPayloadError(DecryptionError):
    pass


class MismatchingRoomError(DecryptionError):
    def __init__(self) -> None:
        super().__init__("Encrypted megolm event is not intended for this room")


class DeviceValidationError(EncryptionError):
    pass