From 79796e8ae9c21561be8571d4a1c6324555022387 Mon Sep 17 00:00:00 2001 From: Johannes Kulick Date: Tue, 11 Dec 2012 18:26:06 +0100 Subject: create PGP/MIME mail, if encrypt flag is set If the envelope has the encrypt flag set we encrypt the (signed if available) mail and create a PGP/MIME compliant mail TODOs: a) create a 'This is a PGP/MIME encrypted mail...' warning on top of all multipart parts b) find the correct key for encryption c) find a way to handle multiple recipients --- alot/db/envelope.py | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) (limited to 'alot/db/envelope.py') diff --git a/alot/db/envelope.py b/alot/db/envelope.py index 96abbd40..7c3668cc 100644 --- a/alot/db/envelope.py +++ b/alot/db/envelope.py @@ -188,7 +188,7 @@ class Envelope(object): raise GPGProblem(str(e)) micalg = crypto.RFC3156_micalg_from_algo(signatures[0].hash_algo) - outer_msg = MIMEMultipart('signed', micalg=micalg, + unencrypted_msg = MIMEMultipart('signed', micalg=micalg, protocol='application/pgp-signature') # wrap signature in MIMEcontainter @@ -200,11 +200,42 @@ class Envelope(object): signature_mime.set_charset('us-ascii') # add signed message and signature to outer message - outer_msg.attach(inner_msg) - outer_msg.attach(signature_mime) - outer_msg['Content-Disposition'] = 'inline' + unencrypted_msg.attach(inner_msg) + unencrypted_msg.attach(signature_mime) + unencrypted_msg['Content-Disposition'] = 'inline' else: - outer_msg = inner_msg + unencrypted_msg = inner_msg + + if self.encrypt: + plaintext = crypto.email_as_string(unencrypted_msg) + logging.debug('encrypting plaintext: ' + plaintext) + + # TODO: find the correct key, or ask user + key = crypto.get_key('recipient') + + try: + encrypted_str = crypto.encrypt(plaintext, key) + except gpgme.GpgmeError as e: + raise GPGProblem(str(e)) + + outer_msg = MIMEMultipart('encrypted', + protocol='application/pgp-encrypted') + + version_str = 'Version: 1' + encryption_mime = MIMEApplication(_data=version_str, + _subtype='pgp-encrypted', + _encoder=encode_7or8bit) + encryption_mime.set_charset('us-ascii') + + encrypted_mime = MIMEApplication(_data=encrypted_str, + _subtype='octet-stream', + _encoder=encode_7or8bit) + encrypted_mime.set_charset('us-ascii') + outer_msg.attach(encryption_mime) + outer_msg.attach(encrypted_mime) + + else: + outer_msg = unencrypted_msg headers = self.headers.copy() # add Message-ID -- cgit v1.2.3