1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
# Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of the PGP Gajim Plugin.
#
# PGP Gajim Plugin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# PGP Gajim Plugin 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
from typing import Any
from typing import TYPE_CHECKING
import logging
import os
from collections.abc import Callable
from functools import partial
import nbxmpp
from packaging.version import Version as V
from gajim.common import app
from gajim.common import ged
from gajim.common.client import Client
from gajim.common.structs import OutgoingMessage
from gajim.gtk.alert import AlertDialog
from gajim.gtk.alert import CancelDialogResponse
from gajim.gtk.alert import DialogCheckButton
from gajim.gtk.alert import DialogResponse
from gajim.gtk.alert import InformationAlertDialog
from gajim.gtk.control import ChatControl
from gajim.plugins import GajimPlugin
from gajim.plugins.plugins_i18n import _
from pgp.exceptions import KeyMismatch
from pgp.gtk.config import PGPConfigDialog
from pgp.gtk.key import KeyDialog
from pgp.modules.events import PGPNotTrusted
from pgp.modules.util import find_gpg
if TYPE_CHECKING:
from pgp.modules.pgp_legacy import PGPLegacy
ENCRYPTION_NAME = "PGP"
log = logging.getLogger("gajim.p.pgplegacy")
error = False
try:
import gnupg
except ImportError:
error = True
else:
# We need https://pypi.python.org/pypi/python-gnupg
# but https://pypi.python.org/pypi/gnupg shares the same package name.
# It cannot be used as a drop-in replacement.
# We test with a version check if python-gnupg is installed as it is
# on a much lower version number than gnupg
# Also we need at least python-gnupg 0.3.8
v_gnupg = gnupg.__version__
if V(v_gnupg) < V("0.3.8") or V(v_gnupg) > V("1.0.0"):
log.error("We need python-gnupg >= 0.3.8")
error = True
error_msg = None
BINARY = find_gpg()
log.info("Found GPG executable: %s", BINARY)
if BINARY is None or error:
if os.name == "nt":
error_msg = _("Please install GnuPG / Gpg4win")
else:
error_msg = _("Please install python-gnupg and gnupg")
class PGPPlugin(GajimPlugin):
def init(self):
self.description = _("PGP encryption as per XEP-0027")
if error_msg:
self.activatable = False
self.config_dialog = None
self.available_text = error_msg
return
self.config_dialog = partial(PGPConfigDialog, self)
self.encryption_name = ENCRYPTION_NAME
self.allow_zeroconf = True
self.gui_extension_points = {
"encrypt" + ENCRYPTION_NAME: (self._encrypt_message, None),
"send_message" + ENCRYPTION_NAME: (self._before_sendmessage, None),
"encryption_dialog" + ENCRYPTION_NAME: (self._on_encryption_dialog, None),
"encryption_state" + ENCRYPTION_NAME: (self._encryption_state, None),
"send-presence": (self._on_send_presence, None),
}
from pgp.modules import pgp_legacy
self.modules = [pgp_legacy]
self.events_handlers = {
"pgp-not-trusted": (ged.PRECORE, self._on_not_trusted),
}
@staticmethod
def get_pgp_module(account: str) -> PGPLegacy:
return app.get_client(account).get_module("PGPLegacy") # pyright: ignore
def activate(self) -> None:
pass
def deactivate(self) -> None:
pass
def activate_encryption(self, chat_control: ChatControl) -> bool:
return True
@staticmethod
def _encryption_state(_chat_control: ChatControl, state: dict[str, Any]) -> None:
state["visible"] = True
state["authenticated"] = True
def _on_encryption_dialog(self, chat_control: ChatControl):
account = chat_control.account
jid = chat_control.contact.jid
transient = app.window
KeyDialog(self, account, jid, transient)
def _on_send_presence(self, account: str, presence: nbxmpp.Presence) -> None:
status = presence.getStatus()
self.get_pgp_module(account).sign_presence(presence, status)
@staticmethod
def _on_not_trusted(event: PGPNotTrusted) -> None:
def _on_response(response_id: str, do_not_ask_again: bool) -> None:
if response_id == "accept":
event.on_yes(do_not_ask_again)
else:
event.on_no()
AlertDialog(
_("Untrusted PGP key"),
_(
"The PGP key used to encrypt this chat is not "
"trusted. Do you really want to encrypt this "
"message?"
),
responses=[
CancelDialogResponse(),
DialogResponse("accept", _("_Encrypt Anyway")),
],
extra_widget=DialogCheckButton(label=_("_Do not ask me again")),
callback=_on_response,
)
def _before_sendmessage(self, chat_control: ChatControl) -> None:
account = chat_control.account
jid = str(chat_control.contact.jid)
pgp = self.get_pgp_module(account)
try:
valid = pgp.has_valid_key_assigned(jid)
except KeyMismatch as announced_key_id:
InformationAlertDialog(
_("PGP Key mismatch"),
_(
"The contact's key (%s) <b>does not match</b> the key "
"assigned in Gajim."
)
% announced_key_id,
)
chat_control.sendmessage = False
return
if not valid:
InformationAlertDialog(
_("No OpenPGP key assigned"),
_("No OpenPGP key is assigned to this contact."),
)
chat_control.sendmessage = False
elif pgp.get_own_key_data() is None:
InformationAlertDialog(
_("No OpenPGP key assigned"),
_("No OpenPGP key is assigned to your account."),
)
chat_control.sendmessage = False
def _encrypt_message(
self,
client: Client,
event: OutgoingMessage,
callback: Callable[[OutgoingMessage], None],
):
self.get_pgp_module(client.name).encrypt_message(client, event, callback)
|