Verified Commit fbdb541b by Tulio Casagrande Committed by meskio

[feat] send public key as attachment on every email

Previously, we were sending the key attached as long as the contact
hasn't replied back. But with new key replace scenarios, we need to updated
the contact keyring with the new key.

We can implement autocrypt or similar in the future, but for now, let's
send the key attached on every email.
parent 85c0e9b9
......@@ -315,23 +315,14 @@ class OutgoingMail(object):
emit_async(catalog.SMTP_START_ENCRYPT_AND_SIGN,
self._from_address,
"%s,%s" % (self._from_address, to_address))
d = self._maybe_attach_key(origmsg, from_address, to_address)
d = self._attach_key(origmsg, from_address)
d.addCallback(maybe_encrypt_and_sign)
return d
def _maybe_attach_key(self, origmsg, from_address, to_address):
def _attach_key(self, origmsg, from_address):
filename = "%s-email-key.asc" % (from_address,)
def attach_if_address_hasnt_encrypted(to_key):
# if the sign_used flag is true that means that we got an encrypted
# email from this address, because we conly check signatures on
# encrypted emails. In this case we don't attach.
# XXX: this might not be true some time in the future
if to_key.sign_used:
return origmsg
return get_key_and_attach(None)
def get_key_and_attach(_):
def get_key_and_attach():
d = self._keymanager.get_key(from_address, fetch_remote=False)
d.addCallback(attach_key)
return d
......@@ -352,8 +343,9 @@ class OutgoingMail(object):
msg.attach(keymsg)
return msg
d = self._keymanager.get_key(to_address, fetch_remote=False)
d.addCallbacks(attach_if_address_hasnt_encrypted, get_key_and_attach)
self.log.info("Will send %s public key as an attachment."
% (from_address))
d = get_key_and_attach()
d.addErrback(lambda _: origmsg)
return d
......
......@@ -96,15 +96,12 @@ class TestOutgoingMail(KeyManagerWithSoledadTestCase):
"""
def check_decryption(res):
decrypted, _ = res
self.assertEqual(
'\n' + self.expected_body,
self.assertIn(
self.expected_body,
decrypted,
'Decrypted text differs from plaintext.')
'Decrypted text does not contain the original text.')
d = self._set_sign_used(ADDRESS)
d.addCallback(
lambda _:
self.outgoing_mail._maybe_encrypt_and_sign(self.raw, self.dest))
d = self.outgoing_mail._maybe_encrypt_and_sign(self.raw, self.dest)
d.addCallback(self._assert_encrypted)
d.addCallback(lambda message: self.km.decrypt(
message.get_payload(1).get_payload(), ADDRESS))
......@@ -118,17 +115,14 @@ class TestOutgoingMail(KeyManagerWithSoledadTestCase):
'"""
def check_decryption_and_verify(res):
decrypted, signkey = res
self.assertEqual(
'\n' + self.expected_body,
self.assertIn(
self.expected_body,
decrypted,
'Decrypted text differs from plaintext.')
'Decrypted text does not contain the original text.')
self.assertTrue(ADDRESS_2 in signkey.address,
"Verification failed")
d = self._set_sign_used(ADDRESS)
d.addCallback(
lambda _:
self.outgoing_mail._maybe_encrypt_and_sign(self.raw, self.dest))
d = self.outgoing_mail._maybe_encrypt_and_sign(self.raw, self.dest)
d.addCallback(self._assert_encrypted)
d.addCallback(lambda message: self.km.decrypt(
message.get_payload(1).get_payload(), ADDRESS, verify=ADDRESS_2))
......@@ -242,15 +236,6 @@ class TestOutgoingMail(KeyManagerWithSoledadTestCase):
return
self.fail("No public key attachment found")
def _set_sign_used(self, address):
def set_sign(key):
key.sign_used = True
return self.km.put_key(key)
d = self.km.get_key(address, fetch_remote=False)
d.addCallback(set_sign)
return d
def _assert_encrypted(self, res):
message, _ = res
self.assertTrue('Content-Type' in message)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment