Skip to content

Commit

Permalink
Make sure MIME crypto wrappers add/update crypto state classes
Browse files Browse the repository at this point in the history
  • Loading branch information
BjarniRunar committed Oct 14, 2014
1 parent 2969b61 commit 784d747
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions mailpile/crypto/gpgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,10 @@ def _encrypt(self, message_text, tokeys=None, armor=False):
tokeys=tokeys, armor=True,
sign=True, fromkey=from_key)

def _update_crypto_status(self, part):
part.signature_info.part_status = 'verified'
part.encryption_info.part_status = 'decrypted'


class GnuPGExpectScript(threading.Thread):
STARTUP = 'Startup'
Expand Down
32 changes: 29 additions & 3 deletions mailpile/crypto/mime.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ def __init__(self, config, cleaner=None, sender=None, recipients=None):
self.sender = sender
self.cleaner = cleaner
self.recipients = recipients or []
self.container = MIMEMultipart()
self.container.set_type(self.CONTAINER_TYPE)
self.container = c = MIMEMultipart()
c.set_type(self.CONTAINER_TYPE)
c.signature_info = SignatureInfo(bubbly=False)
c.encryption_info = EncryptionInfo(bubbly=False)
if self.cleaner:
self.cleaner(self.container)
for pn, pv in self.CONTAINER_PARAMS:
Expand All @@ -243,7 +245,18 @@ def crypto(self):
return NotImplementedError("Please override me")

def attach(self, part):
self.container.attach(part)
c = self.container
c.attach(part)

if not hasattr(part, 'signature_info'):
part.signature_info = SignatureInfo(parent=c.signature_info)
part.encryption_info = EncryptionInfo(parent=c.encryption_info)
else:
part.signature_info.parent = c.signature_info
part.signature_info.bubbly = True
part.encryption_info.parent = c.encryption_info
part.encryption_info.bubbly = True

if self.cleaner:
self.cleaner(part)
del part['MIME-Version']
Expand Down Expand Up @@ -276,6 +289,9 @@ def wrap(self, msg):
if not hl.startswith('content-') and not hl.startswith('mime-'):
self.container[h] = msg[h]
del msg[h]
if hasattr(msg, 'signature_info'):
self.container.signature_info = msg.signature_info
self.container.encryption_info = msg.encryption_info
return self.container


Expand All @@ -295,6 +311,9 @@ def __init__(self, *args, **kwargs):
"attachment; filename=\"signature.asc\"")):
self.sigblock.add_header(h, v)

def _update_crypto_status(self, part):
part.signature_info.part_status = 'verified'

def wrap(self, msg, prefer_inline=False):
from_key = self.get_keys([self.sender])[0]

Expand All @@ -310,6 +329,7 @@ def wrap(self, msg, prefer_inline=False):
armor=True)
if status == 0:
_update_text_payload(prefer_inline, sig)
self._update_crypto_status(prefer_inline)
return msg

else:
Expand All @@ -321,6 +341,7 @@ def wrap(self, msg, prefer_inline=False):
fromkey=from_key, armor=True)
if status == 0:
self.sigblock.set_payload(sig)
self._update_crypto_status(self.container)
return self.container

raise SignatureFailureError(_('Failed to sign message!'))
Expand Down Expand Up @@ -352,6 +373,9 @@ def _encrypt(self, message_text, tokeys=None, armor=False):
return self.crypto().encrypt(message_text,
tokeys=tokeys, armor=True)

def _update_crypto_status(self, part):
part.encryption_info.part_status = 'decrypted'

def wrap(self, msg, prefer_inline=False):
to_keys = set(self.get_keys(self.recipients + [self.sender]))

Expand All @@ -365,6 +389,7 @@ def wrap(self, msg, prefer_inline=False):
armor=True)
if status == 0:
_update_text_payload(prefer_inline, enc)
self._update_crypto_status(prefer_inline)
return msg

else:
Expand All @@ -378,6 +403,7 @@ def wrap(self, msg, prefer_inline=False):
armor=True)
if status == 0:
self.enc_data.set_payload(enc)
self._update_crypto_status(self.enc_data)
return self.container

raise EncryptionFailureError(_('Failed to encrypt message!'))

0 comments on commit 784d747

Please sign in to comment.