From b33f601b2244c5bdf76a57f7581ab71b75474fa6 Mon Sep 17 00:00:00 2001 From: Andrew Engelbrecht Date: Fri, 24 Jul 2015 19:23:33 -0400 Subject: [PATCH] sign reply message even if not encrypting. --- edward | 61 +++++++++++++++------ run-tests | 2 +- tests/{flatten-4.eml => gpg-flatten-10.eml} | 0 tests/{flatten-2.out => gpg-flatten-10.out} | 0 tests/{flatten-1.eml => gpg-flatten-7.eml} | 0 tests/{flatten-1.out => gpg-flatten-7.out} | 0 tests/{flatten-2.eml => gpg-flatten-8.eml} | 0 tests/{flatten-4.out => gpg-flatten-8.out} | 0 tests/{flatten-3.eml => gpg-flatten-9.eml} | 0 tests/{flatten-3.out => gpg-flatten-9.out} | 0 10 files changed, 45 insertions(+), 18 deletions(-) rename tests/{flatten-4.eml => gpg-flatten-10.eml} (100%) rename tests/{flatten-2.out => gpg-flatten-10.out} (100%) rename tests/{flatten-1.eml => gpg-flatten-7.eml} (100%) rename tests/{flatten-1.out => gpg-flatten-7.out} (100%) rename tests/{flatten-2.eml => gpg-flatten-8.eml} (100%) rename tests/{flatten-4.out => gpg-flatten-8.out} (100%) rename tests/{flatten-3.eml => gpg-flatten-9.eml} (100%) rename tests/{flatten-3.out => gpg-flatten-9.out} (100%) diff --git a/edward b/edward index 7eb6e6c..c1901dd 100755 --- a/edward +++ b/edward @@ -1175,27 +1175,30 @@ def generate_encrypted_mime (plaintext, email_from, email_subject, encrypt_to_ke encrypted_text = encrypt_sign_message(plaintext_mime.as_string(), encrypt_to_key, gpgme_ctx) + gpg_payload = encrypted_text - control_mime = MIMEApplication("Version: 1", - _subtype='pgp-encrypted', - _encoder=email.encoders.encode_7or8bit) - control_mime['Content-Description'] = 'PGP/MIME version identification' - control_mime.set_charset('us-ascii') + else: + signed_text = sign_message(plaintext_mime.as_string(), gpgme_ctx) + gpg_payload = signed_text - encoded_mime = MIMEApplication(encrypted_text, - _subtype='octet-stream; name="encrypted.asc"', - _encoder=email.encoders.encode_7or8bit) - encoded_mime['Content-Description'] = 'OpenPGP encrypted message' - encoded_mime['Content-Disposition'] = 'inline; filename="encrypted.asc"' - encoded_mime.set_charset('us-ascii') + control_mime = MIMEApplication("Version: 1", + _subtype='pgp-encrypted', + _encoder=email.encoders.encode_7or8bit) + control_mime['Content-Description'] = 'PGP/MIME version identification' + control_mime.set_charset('us-ascii') - message_mime = MIMEMultipart(_subtype="encrypted", protocol="application/pgp-encrypted") - message_mime.attach(control_mime) - message_mime.attach(encoded_mime) - message_mime['Content-Disposition'] = 'inline' + encoded_mime = MIMEApplication(gpg_payload, + _subtype='octet-stream; name="encrypted.asc"', + _encoder=email.encoders.encode_7or8bit) + encoded_mime['Content-Description'] = 'OpenPGP encrypted message' + encoded_mime['Content-Disposition'] = 'inline; filename="encrypted.asc"' + encoded_mime.set_charset('us-ascii') + + message_mime = MIMEMultipart(_subtype="encrypted", protocol="application/pgp-encrypted") + message_mime.attach(control_mime) + message_mime.attach(encoded_mime) + message_mime['Content-Disposition'] = 'inline' - else: - message_mime = plaintext_mime message_mime['To'] = email_from message_mime['Subject'] = email_subject @@ -1237,6 +1240,7 @@ def encrypt_sign_message (plaintext, encrypt_to_key, gpgme_ctx): An encrypted and signed string of text """ + # the plaintext should be mime encoded in an ascii-compatible form plaintext_bytes = io.BytesIO(plaintext.encode('ascii')) encrypted_bytes = io.BytesIO() @@ -1247,6 +1251,29 @@ def encrypt_sign_message (plaintext, encrypt_to_key, gpgme_ctx): return encrypted_txt +def sign_message (plaintext, gpgme_ctx): + """Signs plaintext + + This signs a message. + + Args: + plaintext: text to sign + gpgme_ctx: the gpgme context + + Returns: + An armored signature as a string of text + """ + + # the plaintext should be mime encoded in an ascii-compatible form + plaintext_bytes = io.BytesIO(plaintext.encode('ascii')) + signed_bytes = io.BytesIO() + + gpgme_ctx.sign(plaintext_bytes, signed_bytes, gpgme.SIG_MODE_NORMAL) + + signed_txt = signed_bytes.getvalue().decode('ascii') + return signed_txt + + def error (error_msg): """Write an error message to stdout diff --git a/run-tests b/run-tests index 715ba4d..8bb5f20 100755 --- a/run-tests +++ b/run-tests @@ -19,7 +19,7 @@ NUM_TESTS=1 #TEST_TYPES="gpg-flatten flatten plain" -TEST_TYPES="gpg-flatten flatten" +TEST_TYPES="gpg-flatten" function _main { diff --git a/tests/flatten-4.eml b/tests/gpg-flatten-10.eml similarity index 100% rename from tests/flatten-4.eml rename to tests/gpg-flatten-10.eml diff --git a/tests/flatten-2.out b/tests/gpg-flatten-10.out similarity index 100% rename from tests/flatten-2.out rename to tests/gpg-flatten-10.out diff --git a/tests/flatten-1.eml b/tests/gpg-flatten-7.eml similarity index 100% rename from tests/flatten-1.eml rename to tests/gpg-flatten-7.eml diff --git a/tests/flatten-1.out b/tests/gpg-flatten-7.out similarity index 100% rename from tests/flatten-1.out rename to tests/gpg-flatten-7.out diff --git a/tests/flatten-2.eml b/tests/gpg-flatten-8.eml similarity index 100% rename from tests/flatten-2.eml rename to tests/gpg-flatten-8.eml diff --git a/tests/flatten-4.out b/tests/gpg-flatten-8.out similarity index 100% rename from tests/flatten-4.out rename to tests/gpg-flatten-8.out diff --git a/tests/flatten-3.eml b/tests/gpg-flatten-9.eml similarity index 100% rename from tests/flatten-3.eml rename to tests/gpg-flatten-9.eml diff --git a/tests/flatten-3.out b/tests/gpg-flatten-9.out similarity index 100% rename from tests/flatten-3.out rename to tests/gpg-flatten-9.out -- 2.25.1