1 #! /usr/bin/env python3
2 # -*- coding: utf-8 -*-
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 ************************************************************************
27 Code sourced from these projects:
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
50 match_types
= [('clearsign',
51 '-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----'),
53 '-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----'),
55 '-----BEGIN PGP PUBLIC KEY BLOCK-----.*?-----END PGP PUBLIC KEY BLOCK-----'),
57 '-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----')]
60 class EddyMsg (object):
62 self
.multipart
= False
66 self
.payload_bytes
= None
67 self
.payload_pieces
= []
70 self
.content_type
= None
71 self
.description_list
= None
74 class PayloadPiece (object):
76 self
.piece_type
= None
81 class GPGData (object):
83 self
.decrypted
= False
94 gpgme_ctx
= get_gpg_context(edward_config
.gnupghome
,
95 edward_config
.sign_with_key
)
97 email_text
= sys
.stdin
.read()
98 result
= parse_pgp_mime(email_text
, gpgme_ctx
)
99 email_from
, email_subject
= email_from_subject(email_text
)
101 print(flatten_eddy(result
))
103 # encrypt_to_key = choose_reply_encryption_key(gpgme_ctx, fingerprints)
105 # reply_message = generate_reply(plaintext, email_from, \
106 # email_subject, encrypt_to_key,
110 def get_gpg_context (gnupghome
, sign_with_key_fp
):
112 os
.environ
['GNUPGHOME'] = gnupghome
114 gpgme_ctx
= gpgme
.Context()
115 gpgme_ctx
.armor
= True
118 sign_with_key
= gpgme_ctx
.get_key(sign_with_key_fp
)
120 error("unable to load signing key. is the gnupghome "
121 + "and signing key properly set in the edward_config.py?")
124 gpgme_ctx
.signers
= [sign_with_key
]
129 def parse_pgp_mime (email_text
, gpgme_ctx
):
131 email_struct
= email
.parser
.Parser().parsestr(email_text
)
133 eddy_obj
= parse_mime(email_struct
)
134 eddy_obj
= split_payloads(eddy_obj
)
135 eddy_obj
= gpg_on_payloads(eddy_obj
, gpgme_ctx
)
140 def parse_mime(msg_struct
):
144 if msg_struct
.is_multipart() == True:
145 payloads
= msg_struct
.get_payload()
147 eddy_obj
.multipart
= True
148 eddy_obj
.subparts
= list(map(parse_mime
, payloads
))
151 eddy_obj
= get_subpart_data(msg_struct
)
156 def scan_and_split (payload_piece
, match_type
, pattern
):
158 # don't try to re-split pieces containing gpg data
159 if payload_piece
.piece_type
!= "text":
160 return [payload_piece
]
162 flags
= re
.DOTALL | re
.MULTILINE
163 matches
= re
.search("(?P<beginning>.*?)(?P<match>" + pattern
+
164 ")(?P<rest>.*)", payload_piece
.string
, flags
=flags
)
167 pieces
= [payload_piece
]
171 beginning
= PayloadPiece()
172 beginning
.string
= matches
.group('beginning')
173 beginning
.piece_type
= payload_piece
.piece_type
175 match
= PayloadPiece()
176 match
.string
= matches
.group('match')
177 match
.piece_type
= match_type
179 rest
= PayloadPiece()
180 rest
.string
= matches
.group('rest')
181 rest
.piece_type
= payload_piece
.piece_type
183 more_pieces
= scan_and_split(rest
, match_type
, pattern
)
184 pieces
= [beginning
, match
] + more_pieces
189 def get_subpart_data (part
):
193 obj
.charset
= part
.get_content_charset()
194 obj
.payload_bytes
= part
.get_payload(decode
=True)
196 obj
.filename
= part
.get_filename()
197 obj
.content_type
= part
.get_content_type()
198 obj
.description_list
= part
['content-description']
200 # your guess is as good as a-myy-ee-ine...
201 if obj
.charset
== None:
202 obj
.charset
= 'utf-8'
204 if obj
.payload_bytes
!= None:
206 payload
= PayloadPiece()
207 payload
.string
= obj
.payload_bytes
.decode(obj
.charset
)
208 payload
.piece_type
= 'text'
210 obj
.payload_pieces
= [payload
]
211 except UnicodeDecodeError:
217 def do_to_eddys_pieces (function_to_do
, eddy_obj
, data
):
222 if eddy_obj
.multipart
== True:
224 for sub
in eddy_obj
.subparts
:
225 result_list
+= do_to_eddys_pieces(function_to_do
, sub
, data
)
227 result_list
= [function_to_do(eddy_obj
, data
)]
232 def split_payloads (eddy_obj
):
234 for match_type
in match_types
:
235 do_to_eddys_pieces(split_payload_pieces
, eddy_obj
, match_type
)
240 def split_payload_pieces (eddy_obj
, match_type
):
242 (match_name
, pattern
) = match_type
245 for piece
in eddy_obj
.payload_pieces
:
246 new_pieces_list
+= scan_and_split(piece
, match_name
, pattern
)
248 eddy_obj
.payload_pieces
= new_pieces_list
251 def gpg_on_payloads (eddy_obj
, gpgme_ctx
):
253 do_to_eddys_pieces(gpg_on_payload_pieces
, eddy_obj
, gpgme_ctx
)
258 def gpg_on_payload_pieces (eddy_obj
, gpgme_ctx
):
260 for piece
in eddy_obj
.payload_pieces
:
262 if piece
.piece_type
== "text":
263 # don't transform the plaintext.
266 elif piece
.piece_type
== "message":
267 (plaintext
, sigs
) = decrypt_block (piece
.string
, gpgme_ctx
)
270 piece
.gpg_data
= GPGData()
271 piece
.gpg_data
.sigs
= sigs
273 piece
.gpg_data
.plainobj
= parse_pgp_mime(plaintext
, gpgme_ctx
)
275 elif piece
.piece_type
== "pubkey":
276 fingerprints
= add_gpg_key(piece
.string
, gpgme_ctx
)
278 if fingerprints
!= []:
279 piece
.gpg_data
= GPGData()
280 piece
.gpg_data
.keys
= fingerprints
282 elif piece
.piece_type
== "clearsign":
283 (plaintext
, fingerprints
) = verify_clear_signature(piece
.string
, gpgme_ctx
)
285 if fingerprints
!= []:
286 piece
.gpg_data
= GPGData()
287 piece
.gpg_data
.sigs
= fingerprints
288 piece
.gpg_data
.plainobj
= parse_pgp_mime(plaintext
, gpgme_ctx
)
294 def flatten_eddy (eddy_obj
):
296 string
= "\n".join(do_to_eddys_pieces(flatten_payload_pieces
, eddy_obj
, None))
301 def flatten_payload_pieces (eddy_obj
, _ignore
):
304 for piece
in eddy_obj
.payload_pieces
:
305 if piece
.piece_type
== "text":
306 string
+= piece
.string
307 elif piece
.gpg_data
== None:
308 string
+= "Hmmm... I wasn't able to get that part.\n"
309 elif piece
.piece_type
== "message":
311 string
+= flatten_eddy(piece
.gpg_data
.plainobj
)
312 elif piece
.piece_type
== "pubkey":
313 string
+= "thanks for your public key:"
314 for key
in piece
.gpg_data
.keys
:
316 elif piece
.piece_type
== "clearsign":
317 string
+= "*** Begin signed part ***\n"
318 string
+= flatten_eddy(piece
.gpg_data
.plainobj
)
319 string
+= "\n*** End signed part ***"
324 def email_from_subject (email_text
):
326 email_struct
= email
.parser
.Parser().parsestr(email_text
)
328 email_from
= email_struct
['From']
329 email_subject
= email_struct
['Subject']
331 return email_from
, email_subject
334 def add_gpg_key (key_block
, gpgme_ctx
):
336 fp
= io
.BytesIO(key_block
.encode('ascii'))
338 result
= gpgme_ctx
.import_(fp
)
339 imports
= result
.imports
344 for import_
in imports
:
345 fingerprint
= import_
[0]
346 fingerprints
+= [fingerprint
]
348 debug("added gpg key: " + fingerprint
)
353 def verify_clear_signature (sig_block
, gpgme_ctx
):
355 # FIXME: this might require the un-decoded bytes
356 # or the correct re-encoding with the carset of the mime part.
357 msg_fp
= io
.BytesIO(sig_block
.encode('utf-8'))
358 ptxt_fp
= io
.BytesIO()
360 result
= gpgme_ctx
.verify(msg_fp
, None, ptxt_fp
)
362 # FIXME: this might require using the charset of the mime part.
363 plaintext
= ptxt_fp
.getvalue().decode('utf-8')
367 fingerprints
+= [res_
.fpr
]
369 return plaintext
, fingerprints
372 def decrypt_block (msg_block
, gpgme_ctx
):
374 block_b
= io
.BytesIO(msg_block
.encode('ascii'))
375 plain_b
= io
.BytesIO()
378 sigs
= gpgme_ctx
.decrypt_verify(block_b
, plain_b
)
382 plaintext
= plain_b
.getvalue().decode('utf-8')
383 return (plaintext
, sigs
)
386 def choose_reply_encryption_key (gpgme_ctx
, fingerprints
):
389 for fp
in fingerprints
:
391 key
= gpgme_ctx
.get_key(fp
)
393 if (key
.can_encrypt
== True):
403 def generate_reply (plaintext
, email_from
, email_subject
, encrypt_to_key
,
407 reply
= "To: " + email_from
+ "\n"
408 reply
+= "Subject: " + email_subject
+ "\n"
410 if (encrypt_to_key
!= None):
411 plaintext_reply
= "thanks for the message!\n\n\n"
412 plaintext_reply
+= email_quote_text(plaintext
)
414 # quoted printable encoding lets most ascii characters look normal
415 # before the decrypted mime message is decoded.
416 char_set
= email
.charset
.Charset("utf-8")
417 char_set
.body_encoding
= email
.charset
.QP
419 # MIMEText doesn't allow setting the text encoding
420 # so we use MIMENonMultipart.
421 plaintext_mime
= MIMENonMultipart('text', 'plain')
422 plaintext_mime
.set_payload(plaintext_reply
, charset
=char_set
)
424 encrypted_text
= encrypt_sign_message(plaintext_mime
.as_string(),
428 control_mime
= MIMEApplication("Version: 1",
429 _subtype
='pgp-encrypted',
430 _encoder
=email
.encoders
.encode_7or8bit
)
431 control_mime
['Content-Description'] = 'PGP/MIME version identification'
432 control_mime
.set_charset('us-ascii')
434 encoded_mime
= MIMEApplication(encrypted_text
,
435 _subtype
='octet-stream; name="encrypted.asc"',
436 _encoder
=email
.encoders
.encode_7or8bit
)
437 encoded_mime
['Content-Description'] = 'OpenPGP encrypted message'
438 encoded_mime
['Content-Disposition'] = 'inline; filename="encrypted.asc"'
439 encoded_mime
.set_charset('us-ascii')
441 message_mime
= MIMEMultipart(_subtype
="encrypted", protocol
="application/pgp-encrypted")
442 message_mime
.attach(control_mime
)
443 message_mime
.attach(encoded_mime
)
444 message_mime
['Content-Disposition'] = 'inline'
446 reply
+= message_mime
.as_string()
450 reply
+= "Sorry, i couldn't find your key.\n"
451 reply
+= "I'll need that to encrypt a message to you."
456 def email_quote_text (text
):
458 quoted_message
= re
.sub(r
'^', r
'> ', text
, flags
=re
.MULTILINE
)
460 return quoted_message
463 def encrypt_sign_message (plaintext
, encrypt_to_key
, gpgme_ctx
):
465 plaintext_bytes
= io
.BytesIO(plaintext
.encode('ascii'))
466 encrypted_bytes
= io
.BytesIO()
468 gpgme_ctx
.encrypt_sign([encrypt_to_key
], gpgme
.ENCRYPT_ALWAYS_TRUST
,
469 plaintext_bytes
, encrypted_bytes
)
471 encrypted_txt
= encrypted_bytes
.getvalue().decode('ascii')
475 def error (error_msg
):
477 sys
.stderr
.write(progname
+ ": " + str(error_msg
) + "\n")
480 def debug (debug_msg
):
482 if edward_config
.debug
== True:
487 if __name__
== "__main__":
490 progname
= sys
.argv
[0]
492 if len(sys
.argv
) > 1:
493 print(progname
+ ": error, this program doesn't " \
494 "need any arguments.", file=sys
.stderr
)