1 #! /usr/bin/env python3
3 """*********************************************************************
4 * Edward is free software: you can redistribute it and/or modify *
5 * it under the terms of the GNU Affero Public License as published by *
6 * the Free Software Foundation, either version 3 of the License, or *
7 * (at your option) any later version. *
9 * Edward is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Affero Public License for more details. *
14 * You should have received a copy of the GNU Affero Public License *
15 * along with Edward. If not, see <http://www.gnu.org/licenses/>. *
17 * Copyright (C) 2014-2015 Andrew Engelbrecht (AGPLv3+) *
18 * Copyright (C) 2014 Josh Drake (AGPLv3+) *
19 * Copyright (C) 2014 Lisa Marie Maginnis (AGPLv3+) *
20 * Copyright (C) 2009-2015 Tails developers <tails@boum.org> ( GPLv3+) *
21 * Copyright (C) 2009 W. Trevor King <wking@drexel.edu> ( GPLv2+) *
23 * Special thanks to Josh Drake for writing the original edward bot! :) *
25 ************************************************************************
29 * http://agpl.fsf.org/emailselfdefense.fsf.org/edward/CURRENT/edward.tar.gz
30 * https://git-tails.immerda.ch/whisperback/tree/whisperBack/encryption.py?h=feature/python3
31 * http://www.physics.drexel.edu/~wking/code/python/send_pgp_mime
44 from email
.mime
.multipart
import MIMEMultipart
45 from email
.mime
.application
import MIMEApplication
46 from email
.mime
.nonmultipart
import MIMENonMultipart
53 email_text
= sys
.stdin
.read()
55 email_from
, email_subject
= email_from_subject(email_text
)
57 plaintext
, keys
= email_decode_flatten(email_text
)
58 encrypt_to_key
= choose_reply_encryption_key(keys
)
60 reply_message
= generate_reply(plaintext
, email_from
, \
61 email_subject
, encrypt_to_key
,
62 "DAB4F989E2788B8DF058E0EFEF1EC52039B36E58")
67 def email_decode_flatten (email_text
):
72 email_struct
= email
.parser
.Parser().parsestr(email_text
)
74 for subpart
in email_struct
.walk():
76 payload
, description
, filename
, content_type \
77 = get_email_subpart_info(subpart
)
82 if content_type
== "multipart":
85 if content_type
== "application/pgp-encrypted":
86 if description
== "PGP/MIME version identification":
87 if payload
.strip() != "Version: 1":
88 print(progname
+ ": Warning: unknown " \
89 + description
+ ": " \
90 + payload
.strip(), file=sys
.stderr
)
94 if (filename
== "encrypted.asc") or (content_type
== "pgp/mime"):
95 plaintext
, more_keys
= decrypt_text(payload
)
100 elif content_type
== "text/plain":
101 body
+= payload
+ "\n"
104 body
+= payload
+ "\n"
109 def email_from_subject (email_text
):
111 email_struct
= email
.parser
.Parser().parsestr(email_text
)
113 email_from
= email_struct
['From']
114 email_subject
= email_struct
['Subject']
116 return email_from
, email_subject
119 def get_email_subpart_info (part
):
121 charset
= part
.get_content_charset()
122 payload_bytes
= part
.get_payload(decode
=True)
124 filename
= part
.get_filename()
125 content_type
= part
.get_content_type()
126 description_list
= part
.get_params(header
='content-description')
131 if payload_bytes
!= None:
132 payload
= payload_bytes
.decode(charset
)
136 if description_list
!= None:
137 description
= description_list
[0][0]
141 return payload
, description
, filename
, content_type
144 def decrypt_text (gpg_text
):
149 gpg_chunks
= split_message(gpg_text
)
151 plaintext_and_sigs_chunks
= decrypt_chunks(gpg_chunks
)
153 for chunk
in plaintext_and_sigs_chunks
:
158 key
= get_pub_key(sig
)
161 # recursive for nested layers of mime and/or gpg
162 plaintext
, more_keys
= email_decode_flatten(plaintext
)
170 def get_pub_key (sig
):
172 gpgme_ctx
= gpgme
.Context()
174 fingerprint
= sig
.fpr
175 key
= gpgme_ctx
.get_key(fingerprint
)
180 def split_message (text
):
182 gpg_matches
= re
.search( \
183 '(-----BEGIN PGP MESSAGE-----' + \
185 '-----END PGP MESSAGE-----)', \
189 if gpg_matches
!= None:
190 gpg_chunks
= gpg_matches
.groups()
197 def decrypt_chunks (gpg_chunks
):
199 return map(decrypt_chunk
, gpg_chunks
)
202 def decrypt_chunk (gpg_chunk
):
204 gpgme_ctx
= gpgme
.Context()
206 chunk_b
= io
.BytesIO(gpg_chunk
.encode('ascii'))
207 plain_b
= io
.BytesIO()
209 sigs
= gpgme_ctx
.decrypt_verify(chunk_b
, plain_b
)
211 plaintext
= plain_b
.getvalue().decode('utf-8')
212 return (plaintext
, sigs
)
215 def choose_reply_encryption_key (keys
):
219 if (key
.can_encrypt
== True):
226 def generate_reply (plaintext
, email_from
, email_subject
, encrypt_to_key
,
227 sign_with_fingerprint
):
230 reply
= "To: " + email_from
+ "\n"
231 reply
+= "Subject: " + email_subject
+ "\n"
233 if (encrypt_to_key
!= None):
234 plaintext_reply
= "thanks for the message!\n\n\n"
235 plaintext_reply
+= email_quote_text(plaintext
)
237 # quoted printable encoding lets most ascii characters look normal
238 # before the decrypted mime message is decoded.
239 char_set
= email
.charset
.Charset("utf-8")
240 char_set
.body_encoding
= email
.charset
.QP
242 # MIMEText doesn't allow setting the text encoding
243 # so we use MIMENonMultipart.
244 plaintext_mime
= MIMENonMultipart('text', 'plain')
245 plaintext_mime
.set_payload(plaintext_reply
, charset
=char_set
)
247 encrypted_text
= encrypt_sign_message(plaintext_mime
.as_string(),
249 sign_with_fingerprint
)
251 control_mime
= MIMEApplication("Version: 1",
252 _subtype
='pgp-encrypted',
253 _encoder
=email
.encoders
.encode_7or8bit
)
254 control_mime
['Content-Description'] = 'PGP/MIME version identification'
255 control_mime
.set_charset('us-ascii')
257 encoded_mime
= MIMEApplication(encrypted_text
,
258 _subtype
='octet-stream; name="encrypted.asc"',
259 _encoder
=email
.encoders
.encode_7or8bit
)
260 encoded_mime
['Content-Description'] = 'OpenPGP encrypted message'
261 encoded_mime
['Content-Disposition'] = 'inline; filename="encrypted.asc"'
262 encoded_mime
.set_charset('us-ascii')
264 message_mime
= MIMEMultipart(_subtype
="encrypted", protocol
="application/pgp-encrypted")
265 message_mime
.attach(control_mime
)
266 message_mime
.attach(encoded_mime
)
267 message_mime
['Content-Disposition'] = 'inline'
269 reply
+= message_mime
.as_string()
273 reply
+= "Sorry, i couldn't find your key.\n"
274 reply
+= "I'll need that to encrypt a message to you."
279 def email_quote_text (text
):
281 quoted_message
= re
.sub(r
'^', r
'> ', text
, flags
=re
.MULTILINE
)
283 return quoted_message
286 def encrypt_sign_message (plaintext
, encrypt_to_key
, sign_with_fingerprint
):
288 gpgme_ctx
= gpgme
.Context()
289 gpgme_ctx
.armor
= True
291 sign_with_key
= gpgme_ctx
.get_key(sign_with_fingerprint
)
292 gpgme_ctx
.signers
= [sign_with_key
]
294 plaintext_bytes
= io
.BytesIO(plaintext
.encode('ascii'))
295 encrypted_bytes
= io
.BytesIO()
297 gpgme_ctx
.encrypt_sign([encrypt_to_key
], gpgme
.ENCRYPT_ALWAYS_TRUST
,
298 plaintext_bytes
, encrypted_bytes
)
300 encrypted_txt
= encrypted_bytes
.getvalue().decode('ascii')
305 if __name__
== "__main__":
308 progname
= sys
.argv
[0]
310 if len(sys
.argv
) > 1:
311 print(progname
+ ": error, this program doesn't " \
312 "need any arguments.", file=sys
.stderr
)