From e645e079cd6fd7eeb3ae28a4e67801fc90139a28 Mon Sep 17 00:00:00 2001 From: Jeff Connelly Date: Tue, 10 Jun 2008 23:36:32 -0700 Subject: [PATCH] Integrate pad rewriting into cli. --- SecureMail.py | 24 +++++++++++++++++++++++- cli.py | 42 ++++++++++++++++++++++++++++++++---------- cotp.py | 10 ++++++++++ 3 files changed, 65 insertions(+), 11 deletions(-) diff --git a/SecureMail.py b/SecureMail.py index 04d4938..d8e379b 100644 --- a/SecureMail.py +++ b/SecureMail.py @@ -142,7 +142,7 @@ class SecureMail(threading.Thread): if "--EMOTP_BEGIN--" not in enc_body: continue - subject_plus_body = cotp.decode(enc_body) + subject_plus_body = cotp.decode(enc_body).strip() if "" in subject_plus_body: # encrypted subject @@ -155,6 +155,7 @@ class SecureMail(threading.Thread): continue msgs.append({"body": body, + "body-enc": enc_body, "fake-subject": fake_subject, "subject": subject, "sender": sender, @@ -174,6 +175,27 @@ class SecureMail(threading.Thread): return self.msgs[k] + def replace(self, k, subject, body): + """Replace the message that 'k' decrypts to with 'new', by + rewriting the pad. The same ciphertext now decrypts to 'new' instead + of what it used to.""" + new = subject + "" + body + ret = cotp.replace(self.msgs[k]["body-enc"] + "\n" + new) + + # Re-decrypt on our side for convenience + subject_plus_body = cotp.decode(self.msgs[k]["body-enc"]).strip() + + if "" in subject_plus_body: + # encrypted subject + subject, body = subject_plus_body.split("") + else: + subject, body = self.msgs[k]["fake-subject"], subject_plus_body + + self.msgs[k]["subject"] = subject + self.msgs[k]["body"] = body + + return ret + def send(self, to, subject, body): """Send, in a timeslot if channel filling is enabled, or immediately if channel filling is disabled.""" diff --git a/cli.py b/cli.py index 5282dfc..b3b486d 100644 --- a/cli.py +++ b/cli.py @@ -19,8 +19,28 @@ h Help l List messages q Quit r# Read message number # +w Write a new message +x# Replace what an old message decrypts to """ +def read_message(): + subject = raw_input("Subject: ") + body = multiline_input("Enter body, ending with '.' on a line by itself.") + + return subject, body + +def multiline_input(prompt=None): + """Read possibly multiple lines of input, terminated by '.'.""" + if prompt is not None: + print prompt + inp = "" + while True: + line = raw_input() + if line == ".": + break + inp += line + "\r\n" + return inp + def main(): global ms ms = SecureMail.SecureMail() @@ -46,16 +66,9 @@ def main(): raise SystemExit elif line[0] == "w": to = raw_input("To: ") - subject = raw_input("Subject: ") - print "Enter body, ending with '.' on a line by itself." - body = "" - while True: - line = raw_input() - if line == ".": - break - body += line + "\r\n" - print ms.send(to, subject, body) + subject, body = read_message() + print ms.send(to, subject, body) elif line[0] == "r": try: num = int(line[1:]) @@ -73,9 +86,18 @@ def main(): print "From: %s" % (msg["sender"],) print "Subject: %s" % (msg["subject"],) print msg["body"] + elif line[0] == "x": + try: + num = int(line[1:]) + except: + print "Usage: x#" + continue + print "After-The-Fact Message Replacement" + subject, body = read_message() + print ms.replace(num, subject, body) else: print "Unknown command, type h for help" - + if __name__ == "__main__": main() diff --git a/cotp.py b/cotp.py index 9476f68..586b591 100644 --- a/cotp.py +++ b/cotp.py @@ -11,6 +11,16 @@ def decode(ct): raise errors return pt +def replace(ct_plus_new): + o, i, e = popen2.popen3("./cotp -r") + i.write(ct_plus_new) + i.close() + pt = o.read() + errors = e.read() + if errors: + raise errors + return True # success + # TODO: support padname def encode(pt): o, i, e = popen2.popen3("./cotp -e") -- 2.11.4.GIT