Debian Codegraph @

libervia/backend/plugins/plugin_sec_autocrypt.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # Libervia plugin
4 # Copyright (C) 2009-2026 Jérôme Poisson ([email protected])
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import base64scip-python python python-stdlib 3.11 base64/__init__:
20 from functoolsscip-python python python-stdlib 3.11 functools/__init__: import partialscip-python python python-stdlib 3.11 functools/partial#
21 from typingscip-python python python-stdlib 3.11 typing/__init__: import TYPE_CHECKINGscip-python python python-stdlib 3.11 typing/TYPE_CHECKING.TYPE_CHECKING., castscip-python python python-stdlib 3.11 typing/cast().
22 from typingscip-python python python-stdlib 3.11 typing/__init__: import Literalscip-python python python-stdlib 3.11 typing/Literal.
23
24 from pydanticscip-python python pydantic 2.13.4 pydantic/__init__: import BaseModel, field_validator
25 from twisted.internetscip-python python Twisted 26.4.0 `twisted.internet`/__init__: import deferscip-python python Twisted 26.4.0 `twisted.internet.defer`/__init__:
26 from twisted.words.protocols.jabberscip-python python Twisted 26.4.0 `twisted.words.protocols.jabber`/__init__: import jidscip-python python Twisted 26.4.0 `twisted.words.protocols.jabber.jid`/__init__:
27 from twisted.words.xishscip-python python Twisted 26.4.0 `twisted.words.xish`/__init__: import domishscip-python python Twisted 26.4.0 `twisted.words.xish.domish`/__init__:
28
29 from libervia.backend.core.constantsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/__init__: import Const as Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#
30 from libervia.backend.core.core_typesscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/__init__: import MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy., SatXMPPEntityscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/SatXMPPEntity#
31 from libervia.backend.core.i18nscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.i18n`/__init__: import _scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.i18n`/_.
32 from libervia.backend.core.logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/__init__: import getLoggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/getLogger().
33 from libervia.backend.memoryscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.memory`/__init__: import persistentscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.memory.persistent`/__init__:
34 from libervia.backend.plugins.plugin_xep_0106scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0106`/__init__: import XEP_0106scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0106`/XEP_0106#
35 from libervia.backend.plugins.plugin_xep_0131scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0131`/__init__: import XEP_0131scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0131`/XEP_0131#
36 from libervia.backend.plugins.plugin_xep_0373scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0373`/__init__: import get_gpg_providerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0373`/get_gpg_provider().
37 from libervia.backend.tools.commonscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.tools.common`/__init__: import regexscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.tools.common.regex`/__init__:
38
39 if TYPE_CHECKINGscip-python python python-stdlib 3.11 typing/TYPE_CHECKING.TYPE_CHECKING.:
40 from libervia.backend.core.mainscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/__init__: import LiberviaBackendscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#
41
42 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log. = getLoggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/getLogger().(__name__scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/)
43
44
45 PLUGIN_INFOscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/PLUGIN_INFO. = {
46 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_NAMEscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_NAME.: "Autocrypt",
47 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_IMPORT_NAMEscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_IMPORT_NAME.: "AUTOCRYPT",
48 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_TYPEscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_TYPE.: Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PLUG_TYPE_SECscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PLUG_TYPE_SEC.,
49 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_MODESscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_MODES.: Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PLUG_MODE_BOTHscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PLUG_MODE_BOTH.,
50 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_PROTOCOLSscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_PROTOCOLS.: [],
51 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_DEPENDENCIESscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_DEPENDENCIES.: ["XEP-0106", "XEP-0131", "XEP-0373"],
52 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_RECOMMENDATIONSscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_RECOMMENDATIONS.: [],
53 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_MAINscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_MAIN.: "Autocrypt",
54 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_HANDLERscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_HANDLER.: "no",
55 Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_DESCRIPTIONscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_DESCRIPTION.: _(
56 "Autocrypt support, to automatically encrypt message to email gateways when "
57 "suitable."
58 ),
59 }
60
61
62 class AutocryptHeaderParseErrorscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptHeaderParseError#(ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#):
63 """Raised when Autocrypt header parsing fails"""
64
65
66 class AutocryptDatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#(BaseModel):
67 """Parsed Autocrypt header data.
68
69 @param addr: Email address for the key.
70 @param keydata: Base64-encoded public key.
71 @param prefer_encrypt: Encryption preference hint.
72 """
73
74 addrscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#addr.: strscip-python python python-stdlib 3.11 builtins/str#
75 keydatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#keydata.: strscip-python python python-stdlib 3.11 builtins/str#
76 prefer_encryptscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#prefer_encrypt.: Literalscip-python python python-stdlib 3.11 typing/Literal.["mutual"] | None = None
77
78 @field_validator("addr")
79 @classmethodscip-python python python-stdlib 3.11 builtins/classmethod#
80 def check_emailscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#check_email().(clsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#check_email().(cls), valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#check_email().(value)):
81 valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#check_email().(value) = valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#check_email().(value).strip()
82 if not regexscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.tools.common.regex`/__init__:.RE_EMAILscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.tools.common.regex`/RE_EMAIL..matchscip-python python python-stdlib 3.11 re/Pattern#match().(valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#check_email().(value)):
83 raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#("Invalid email address")
84 return valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#check_email().(value)
85
86 @field_validator("keydata")
87 @classmethodscip-python python python-stdlib 3.11 builtins/classmethod#
88 def validate_keydatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#validate_keydata().(clsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#validate_keydata().(cls), valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#validate_keydata().(value): strscip-python python python-stdlib 3.11 builtins/str#) -> strscip-python python python-stdlib 3.11 builtins/str#:
89 """Validate keydata is proper base64"""
90 try:
91 base64scip-python python python-stdlib 3.11 base64/__init__:.b64decodescip-python python python-stdlib 3.11 base64/b64decode().(valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#validate_keydata().(value), validatescip-python python python-stdlib 3.11 base64/b64decode().(validate)=True)
92 except ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError# as e:
93 raise ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#("Invalid base64 in keydata") from e
94 return valuescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#validate_keydata().(value).stripscip-python python python-stdlib 3.11 builtins/str#strip().()
95
96 def to_headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#to_header().(selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#to_header().(self)) -> strscip-python python python-stdlib 3.11 builtins/str#:
97 """Generate the Autocrypt header.
98
99 @return: Formatted header value per Autocrypt specification.
100 """
101 parts = [f"addr={selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#to_header().(self).addrscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#addr.}", f"keydata={selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#to_header().(self).keydatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#keydata.}"]
102
103 if selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#to_header().(self).prefer_encryptscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#prefer_encrypt. is not None:
104 parts.appendscip-python python python-stdlib 3.11 builtins/list#append().(f"prefer-encrypt={selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#to_header().(self).prefer_encryptscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#prefer_encrypt.}")
105
106 return "; ".joinscip-python python python-stdlib 3.11 builtins/str#join().(parts)
107
108
109 def parse_autocrypt_headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/parse_autocrypt_header().(headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/parse_autocrypt_header().(header): strscip-python python python-stdlib 3.11 builtins/str#) -> AutocryptDatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#:
110 """Parse an Autocrypt header.
111
112 @param header: Raw Autocrypt header value
113 @return: Parsed AutocryptData.
114 @raise AutocryptHeaderParseError: Some required field is invalid or missing.
115 """
116 attributes = {}
117 for part in headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/parse_autocrypt_header().(header).splitscip-python python python-stdlib 3.11 builtins/str#split().(";"):
118 part = part.stripscip-python python python-stdlib 3.11 builtins/str#strip().()
119 if not part:
120 continue
121 if "=" not in part:
122 # Ignore invalid parts
123 continue
124 key, value = part.splitscip-python python python-stdlib 3.11 builtins/str#split().("=", 1)
125 key = key.stripscip-python python python-stdlib 3.11 builtins/str#strip().().lowerscip-python python python-stdlib 3.11 builtins/str#lower().()
126 value = value.stripscip-python python python-stdlib 3.11 builtins/str#strip().()
127
128 if key in {"addr", "keydata", "prefer-encrypt"}:
129 attributes[key] = value
130
131 if not attributes.getscip-python python python-stdlib 3.11 builtins/dict#get().("addr"):
132 raise AutocryptHeaderParseErrorscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptHeaderParseError#('Missing required "addr" attribute')
133 if not attributes.getscip-python python python-stdlib 3.11 builtins/dict#get().("keydata"):
134 raise AutocryptHeaderParseErrorscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptHeaderParseError#('Missing required "keydata" attribute')
135
136 try:
137 return AutocryptDatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#(
138 addrscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#addr.=attributes["addr"],
139 keydatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#keydata.=attributes["keydata"],
140 prefer_encryptscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#prefer_encrypt.=attributes.getscip-python python python-stdlib 3.11 builtins/dict#get().("prefer-encrypt"),
141 )
142 except ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError# as e:
143 raise AutocryptHeaderParseErrorscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptHeaderParseError#(f"Invalid Autocrypt header: {e}") from e
144
145
146 class Autocryptscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#:
147
148 def __init__scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(self), hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(host): "LiberviaBackendscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#") -> None:
149 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(self).hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#host. = hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(host)
150 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..infoscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#info().(f"plugin {PLUGIN_INFOscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/PLUGIN_INFO.[Cscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#.PI_NAMEscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.constants`/Const#PI_NAME.]!r} initialization")
151 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(self)._shimscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_shim. = castscip-python python python-stdlib 3.11 typing/cast().(XEP_0131scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0131`/XEP_0131#, hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(host).pluginsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#plugins.["XEP-0131"])
152 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(self)._escip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_e. = castscip-python python python-stdlib 3.11 typing/cast().(XEP_0106scip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0106`/XEP_0106#, hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(host).pluginsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#plugins.["XEP-0106"])
153 hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(host).triggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#trigger..addscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.tools.trigger`/TriggerManager#add().("message_received", selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(self).message_received_triggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().)
154 hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(host).triggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#trigger..addscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.tools.trigger`/TriggerManager#add().("sendMessage", selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#__init__().(self).send_message_triggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().)
155
156 def profile_connectingscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#profile_connecting().(selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#profile_connecting().(self), clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#profile_connecting().(client): SatXMPPEntityscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/SatXMPPEntity#) -> None:
157 # Sender already handled.
158 clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#profile_connecting().(client)._autocrypt_seen: setscip-python python python-stdlib 3.11 builtins/set#[strscip-python python python-stdlib 3.11 builtins/str#] = setscip-python python python-stdlib 3.11 builtins/set#()
159 clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#profile_connecting().(client)._autocrypt_gpg_provider = get_gpg_providerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0373`/get_gpg_provider().(selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#profile_connecting().(self).hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#host., clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#profile_connecting().(client))
160
161 async def handle_autocrypt_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(
162 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(self), clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(client): SatXMPPEntityscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/SatXMPPEntity#, mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(mess_data): MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy., autocrypt_data_rawscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(autocrypt_data_raw): strscip-python python python-stdlib 3.11 builtins/str#
163 ) -> None:
164 """Process Autocrypt header from XMPP email gateway
165
166 @param client: Client session.
167 @param mess_data: Message data.
168 @param autocrypt_data: Raw Autocrypt header value
169 @raise AutocryptHeaderParseError: For invalid header format
170 """
171 from_jid = mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(mess_data)["from"]
172 to_jid = mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(mess_data)["to"]
173 is_email_gateway = await selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(self).hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#host..memoryscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#memory..discoscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.memory.memory`/Memory#disco..has_identityscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.memory.disco`/Discovery#has_identity().(
174 clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(client), "gateway", "smtp", jidscip-python python Twisted 26.4.0 `twisted.words.protocols.jabber.jid`/__init__:.JID(to_jid.host)
175 )
176 if to_jid.resource or not is_email_gateway:
177 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..warningscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#warning().("Ignoring Autocrypt header from non email gateway.")
178 return
179
180 try:
181 autocrypt_data = parse_autocrypt_headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/parse_autocrypt_header().(autocrypt_data_rawscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(autocrypt_data_raw))
182 except AutocryptHeaderParseErrorscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptHeaderParseError# as e:
183 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..errorscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#error().(f"Invalid Autocrypt header: {e}")
184 return
185
186 sender_email = selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(self)._escip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_e..unescapescip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_xep_0106`/XEP_0106#unescape().(from_jid.user)
187 if sender_email != autocrypt_data.addrscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#addr.:
188 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..warningscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#warning().(
189 f"Sender email ({sender_email!r}) doesn't match autocrypt header address"
190 f" ({autocrypt_data.addrscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#addr.!r}), ignoring autocrypt data."
191 )
192 return
193
194 if sender_email in clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(client)._autocrypt_seen:
195 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..debugscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#debug().(f"We have already handled {sender_email!r} , nothing to do.")
196 return None
197 gpg_provider = clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(client)._autocrypt_gpg_provider
198 public_keys = gpg_provider.list_public_keys(sender_email)
199 if not public_keys:
200 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..debugscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#debug().(
201 f"No public key found for {sender_email!r}, importing autocrypt data."
202 )
203 # FIXME: Maybe we should import the Autocrypt key in a separated location?
204 # Autocrypt is less secure than normal key management.
205 gpg_provider.import_public_key(base64scip-python python python-stdlib 3.11 base64/__init__:.b64decodescip-python python python-stdlib 3.11 base64/b64decode().(autocrypt_data.keydatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#keydata.))
206 else:
207 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..debugscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#debug().(
208 f"There are already known public key for {sender_email}, we skipt "
209 "autocrypt"
210 )
211 clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(client)._autocrypt_seen.add(sender_email)
212
213 def _check_headersscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(
214 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(self), clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(client): SatXMPPEntityscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/SatXMPPEntity#, mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(mess_data): MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy.
215 ) -> MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy.:
216 try:
217 autocrypt_data = mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(mess_data)["extra"]["headers"]["autocrypt"]
218 except KeyErrorscip-python python python-stdlib 3.11 builtins/KeyError#:
219 pass
220 else:
221 deferscip-python python Twisted 26.4.0 `twisted.internet.defer`/__init__:.ensureDeferred(
222 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(self).handle_autocrypt_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#handle_autocrypt_data().(clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(client), mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(mess_data), autocrypt_data)
223 )
224 return mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers().(mess_data)
225
226 def message_received_triggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(
227 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(self),
228 clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(client): SatXMPPEntityscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/SatXMPPEntity#,
229 message_eltscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(message_elt): domishscip-python python Twisted 26.4.0 `twisted.words.xish.domish`/__init__:.Element,
230 post_treatscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(post_treat): deferscip-python python Twisted 26.4.0 `twisted.internet.defer`/__init__:.Deferred,
231 ) -> Literalscip-python python python-stdlib 3.11 typing/Literal.[True]:
232 post_treatscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(post_treat).addCallback(partialscip-python python python-stdlib 3.11 functools/partial#(selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(self)._check_headersscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#_check_headers()., clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#message_received_trigger().(client)))
233 return True
234
235 async def add_autocrypt_headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(
236 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(self), clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(client), mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(mess_data): MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy.
237 ) -> MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy.:
238 to_jid = mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(mess_data)["to"]
239 if await selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(self).hostscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#host..memoryscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.main`/LiberviaBackend#memory..discoscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.memory.memory`/Memory#disco..has_identityscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.memory.disco`/Discovery#has_identity().(
240 clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(client), "gateway", "smtp", jidscip-python python Twisted 26.4.0 `twisted.words.protocols.jabber.jid`/__init__:.JID(to_jid.host)
241 ):
242 gpg_provider = clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(client)._autocrypt_gpg_provider
243 # FIXME! We currently use from jid as email, but we would need to get sender
244 # email from gateway instead, as we don't know what is actually used, and it
245 # may differ from the JID.
246 sender_email = mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(mess_data)["from"].userhost()
247 try:
248 public_key = nextscip-python python python-stdlib 3.11 builtins/next().(iterscip-python python python-stdlib 3.11 builtins/iter().(gpg_provider.list_public_keys(sender_email)))
249 except StopIterationscip-python python python-stdlib 3.11 builtins/StopIteration#:
250 logscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/log..debugscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.log`/Logger#debug().("No public key found, can't set autocrypt header.")
251 return mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(mess_data)
252
253 exported_key = gpg_provider.export_public_key(public_key)
254 autocrypt_data = AutocryptDatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#(
255 addrscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#addr.=sender_email,
256 keydatascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#keydata.=base64scip-python python python-stdlib 3.11 base64/__init__:.b64encodescip-python python python-stdlib 3.11 base64/b64encode().(exported_key).decodescip-python python python-stdlib 3.11 builtins/bytes#decode().("ascii"),
257 prefer_encryptscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#prefer_encrypt.="mutual",
258 )
259
260 mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(mess_data)["extra"].setdefault("headers", {})[
261 "autocrypt"
262 ] = autocrypt_data.to_headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/AutocryptData#to_header().()
263 return mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(mess_data)
264
265 def send_message_triggerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(
266 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(self), clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(client), mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(mess_data), pre_xml_treatmentsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(pre_xml_treatments), post_xml_treatmentsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(post_xml_treatments)
267 ) -> Literalscip-python python python-stdlib 3.11 typing/Literal.[True]:
268 """Process the XEP-0131 related data to be sent"""
269
270 def add_headersscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_headers().(mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_headers().(mess_data): MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy.) -> MessageDataLegacyscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.core.core_types`/MessageDataLegacy.:
271 extra = mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_headers().(mess_data)["extra"]
272 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(self).move_keywords_to_headers(extra)
273 # Now we parse headers, if any.
274 if "headers" in extra:
275 headers_data = HeadersData(**extra["headers"])
276 message_elt = mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_headers().(mess_data)["xml"]
277 message_elt.addChild(headers_data.to_element())
278 return mess_datascip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_headers().(mess_data)
279
280 post_xml_treatmentsscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(post_xml_treatments).addCallback(
281 lambda mess_data: deferscip-python python Twisted 26.4.0 `twisted.internet.defer`/__init__:.ensureDeferred(
282 selfscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(self).add_autocrypt_headerscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#add_autocrypt_header().(clientscip-python python libervia-backend 0.9.0.dev0 `libervia.backend.plugins.plugin_sec_autocrypt`/Autocrypt#send_message_trigger().(client), mess_data)
283 )
284 )
285 return True