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
45 from email
.mime
.multipart
import MIMEMultipart
46 from email
.mime
.application
import MIMEApplication
47 from email
.mime
.nonmultipart
import MIMENonMultipart
51 langs
= ["an", "de", "el", "en", "fr", "ja", "pt-br", "ro", "ru", "tr"]
53 match_types
= [('clearsign',
54 '-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----'),
56 '-----BEGIN PGP MESSAGE-----.*?-----END PGP MESSAGE-----'),
58 '-----BEGIN PGP PUBLIC KEY BLOCK-----.*?-----END PGP PUBLIC KEY BLOCK-----'),
60 '-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----')]
63 class EddyMsg (object):
65 self
.multipart
= False
69 self
.payload_bytes
= None
70 self
.payload_pieces
= []
73 self
.content_type
= None
74 self
.description_list
= None
77 class PayloadPiece (object):
79 self
.piece_type
= None
84 class GPGData (object):
86 self
.decrypted
= False
92 class ReplyInfo (object):
95 self
.msg_to_quote
= ""
97 self
.success_decrypt
= False
98 self
.failed_decrypt
= False
99 self
.public_key_received
= False
100 self
.no_public_key
= False
101 self
.sig_success
= False
102 self
.sig_failure
= False
109 gpgme_ctx
= get_gpg_context(edward_config
.gnupghome
,
110 edward_config
.sign_with_key
)
112 email_text
= sys
.stdin
.read()
113 email_struct
= parse_pgp_mime(email_text
, gpgme_ctx
)
115 email_to
, email_from
, email_subject
= email_to_from_subject(email_text
)
116 lang
= import_lang(email_to
)
118 replyinfo_obj
= ReplyInfo()
119 replyinfo_obj
.replies
= lang
.replies
121 prepare_for_reply(email_struct
, replyinfo_obj
)
122 reply_plaintext
= write_reply(replyinfo_obj
)
124 print(reply_plaintext
)
126 # encrypt_to_key = choose_reply_encryption_key(gpgme_ctx, fingerprints)
128 # reply_mime = generate_encrypted_mime(plaintext, email_from, \
129 # email_subject, encrypt_to_key,
133 def get_gpg_context (gnupghome
, sign_with_key_fp
):
135 os
.environ
['GNUPGHOME'] = gnupghome
137 gpgme_ctx
= gpgme
.Context()
138 gpgme_ctx
.armor
= True
141 sign_with_key
= gpgme_ctx
.get_key(sign_with_key_fp
)
143 error("unable to load signing key. is the gnupghome "
144 + "and signing key properly set in the edward_config.py?")
147 gpgme_ctx
.signers
= [sign_with_key
]
152 def parse_pgp_mime (email_text
, gpgme_ctx
):
154 email_struct
= email
.parser
.Parser().parsestr(email_text
)
156 eddymsg_obj
= parse_mime(email_struct
)
157 split_payloads(eddymsg_obj
)
158 gpg_on_payloads(eddymsg_obj
, gpgme_ctx
)
163 def parse_mime(msg_struct
):
165 eddymsg_obj
= EddyMsg()
167 if msg_struct
.is_multipart() == True:
168 payloads
= msg_struct
.get_payload()
170 eddymsg_obj
.multipart
= True
171 eddymsg_obj
.subparts
= list(map(parse_mime
, payloads
))
174 eddymsg_obj
= get_subpart_data(msg_struct
)
179 def scan_and_split (payload_piece
, match_type
, pattern
):
181 # don't try to re-split pieces containing gpg data
182 if payload_piece
.piece_type
!= "text":
183 return [payload_piece
]
185 flags
= re
.DOTALL | re
.MULTILINE
186 matches
= re
.search("(?P<beginning>.*?)(?P<match>" + pattern
+
187 ")(?P<rest>.*)", payload_piece
.string
, flags
=flags
)
190 pieces
= [payload_piece
]
194 beginning
= PayloadPiece()
195 beginning
.string
= matches
.group('beginning')
196 beginning
.piece_type
= payload_piece
.piece_type
198 match
= PayloadPiece()
199 match
.string
= matches
.group('match')
200 match
.piece_type
= match_type
202 rest
= PayloadPiece()
203 rest
.string
= matches
.group('rest')
204 rest
.piece_type
= payload_piece
.piece_type
206 more_pieces
= scan_and_split(rest
, match_type
, pattern
)
207 pieces
= [beginning
, match
] + more_pieces
212 def get_subpart_data (part
):
216 obj
.charset
= part
.get_content_charset()
217 obj
.payload_bytes
= part
.get_payload(decode
=True)
219 obj
.filename
= part
.get_filename()
220 obj
.content_type
= part
.get_content_type()
221 obj
.description_list
= part
['content-description']
223 # your guess is as good as a-myy-ee-ine...
224 if obj
.charset
== None:
225 obj
.charset
= 'utf-8'
227 if obj
.payload_bytes
!= None:
229 payload
= PayloadPiece()
230 payload
.string
= obj
.payload_bytes
.decode(obj
.charset
)
231 payload
.piece_type
= 'text'
233 obj
.payload_pieces
= [payload
]
234 except UnicodeDecodeError:
240 def do_to_eddys_pieces (function_to_do
, eddymsg_obj
, data
):
242 if eddymsg_obj
.multipart
== True:
243 for sub
in eddymsg_obj
.subparts
:
244 do_to_eddys_pieces(function_to_do
, sub
, data
)
246 function_to_do(eddymsg_obj
, data
)
249 def split_payloads (eddymsg_obj
):
251 for match_type
in match_types
:
252 do_to_eddys_pieces(split_payload_pieces
, eddymsg_obj
, match_type
)
255 def split_payload_pieces (eddymsg_obj
, match_type
):
257 (match_name
, pattern
) = match_type
260 for piece
in eddymsg_obj
.payload_pieces
:
261 new_pieces_list
+= scan_and_split(piece
, match_name
, pattern
)
263 eddymsg_obj
.payload_pieces
= new_pieces_list
266 def gpg_on_payloads (eddymsg_obj
, gpgme_ctx
, prev_parts
=[]):
268 if eddymsg_obj
.multipart
== True:
270 for sub
in eddymsg_obj
.subparts
:
271 gpg_on_payloads (sub
, gpgme_ctx
, prev_parts
)
276 for piece
in eddymsg_obj
.payload_pieces
:
278 if piece
.piece_type
== "text":
279 # don't transform the plaintext.
282 elif piece
.piece_type
== "message":
283 (plaintext
, sigs
) = decrypt_block (piece
.string
, gpgme_ctx
)
286 piece
.gpg_data
= GPGData()
287 piece
.gpg_data
.sigs
= sigs
289 piece
.gpg_data
.plainobj
= parse_pgp_mime(plaintext
, gpgme_ctx
)
291 elif piece
.piece_type
== "pubkey":
292 key_fps
= add_gpg_key(piece
.string
, gpgme_ctx
)
295 piece
.gpg_data
= GPGData()
296 piece
.gpg_data
.keys
= key_fps
298 elif piece
.piece_type
== "clearsign":
299 (plaintext
, sig_fps
) = verify_clear_signature(piece
.string
, gpgme_ctx
)
302 piece
.gpg_data
= GPGData()
303 piece
.gpg_data
.sigs
= sig_fps
304 piece
.gpg_data
.plainobj
= parse_pgp_mime(plaintext
, gpgme_ctx
)
306 elif piece
.piece_type
== "detachedsig":
307 for prev
in prev_parts
:
308 payload_bytes
= prev
.payload_bytes
309 sig_fps
= verify_detached_signature(piece
.string
, payload_bytes
, gpgme_ctx
)
312 piece
.gpg_data
= GPGData()
313 piece
.gpg_data
.sigs
= sig_fps
314 piece
.gpg_data
.plainobj
= prev
320 def prepare_for_reply (eddymsg_obj
, replyinfo_obj
):
322 do_to_eddys_pieces(prepare_for_reply_pieces
, eddymsg_obj
, replyinfo_obj
)
325 def prepare_for_reply_pieces (eddymsg_obj
, replyinfo_obj
):
327 for piece
in eddymsg_obj
.payload_pieces
:
328 if piece
.piece_type
== "text":
329 # don't quote the plaintext part.
332 elif piece
.piece_type
== "message":
333 if piece
.gpg_data
== None:
334 replyinfo_obj
.failed_decrypt
= True
336 replyinfo_obj
.success_decrypt
= True
337 # TODO: only quote it if it is also signed by the encrypter.
338 replyinfo_obj
.msg_to_quote
+= flatten_payloads(piece
.gpg_data
.plainobj
)
340 prepare_for_reply(piece
.gpg_data
.plainobj
, replyinfo_obj
)
342 elif piece
.piece_type
== "pubkey":
343 if piece
.gpg_data
== None:
344 replyinfo_obj
.no_public_key
= True
346 replyinfo_obj
.public_key_received
= True
348 elif (piece
.piece_type
== "clearsign") \
349 or (piece
.piece_type
== "detachedsig"):
350 if piece
.gpg_data
== None:
351 replyinfo_obj
.sig_failure
= True
353 replyinfo_obj
.sig_success
= True
356 def flatten_payloads (eddymsg_obj
):
360 if eddymsg_obj
== None:
363 if eddymsg_obj
.multipart
== True:
364 for sub
in eddymsg_obj
.subparts
:
365 flat_string
+= flatten_payloads (sub
)
369 for piece
in eddymsg_obj
.payload_pieces
:
370 if piece
.piece_type
== "text":
371 flat_string
+= piece
.string
372 elif piece
.piece_type
== "message":
373 flat_string
+= flatten_payloads(piece
.plainobj
)
374 elif ((piece
.piece_type
== "clearsign") \
375 or (piece
.piece_type
== "detachedsig")) \
376 and (piece
.gpg_data
!= None):
377 flat_string
+= flatten_payloads (piece
.gpg_data
.plainobj
)
383 def write_reply (replyinfo_obj
):
387 if replyinfo_obj
.success_decrypt
== True:
388 quoted_text
= email_quote_text(replyinfo_obj
.msg_to_quote
)
389 reply_plain
+= replyinfo_obj
.replies
['success_decrypt']
390 reply_plain
+= quoted_text
392 elif replyinfo_obj
.failed_decrypt
== True:
393 reply_plain
+= replyinfo_obj
.replies
['failed_decrypt']
396 if replyinfo_obj
.sig_success
== True:
397 reply_plain
+= "\n\n"
398 reply_plain
+= replyinfo_obj
.replies
['sig_success']
400 elif replyinfo_obj
.sig_failure
== True:
401 reply_plain
+= "\n\n"
402 reply_plain
+= replyinfo_obj
.replies
['sig_failure']
405 if replyinfo_obj
.public_key_received
== True:
406 reply_plain
+= "\n\n"
407 reply_plain
+= replyinfo_obj
.replies
['public_key_received']
409 elif replyinfo_obj
.no_public_key
== True:
410 reply_plain
+= "\n\n"
411 reply_plain
+= replyinfo_obj
.replies
['no_public_key']
414 reply_plain
+= "\n\n"
415 reply_plain
+= replyinfo_obj
.replies
['signature']
420 def add_gpg_key (key_block
, gpgme_ctx
):
422 fp
= io
.BytesIO(key_block
.encode('ascii'))
424 result
= gpgme_ctx
.import_(fp
)
425 imports
= result
.imports
427 key_fingerprints
= []
430 for import_
in imports
:
431 fingerprint
= import_
[0]
432 key_fingerprints
+= [fingerprint
]
434 debug("added gpg key: " + fingerprint
)
436 return key_fingerprints
439 def verify_clear_signature (sig_block
, gpgme_ctx
):
441 # FIXME: this might require the un-decoded bytes
442 # or the correct re-encoding with the carset of the mime part.
443 msg_fp
= io
.BytesIO(sig_block
.encode('utf-8'))
444 ptxt_fp
= io
.BytesIO()
446 result
= gpgme_ctx
.verify(msg_fp
, None, ptxt_fp
)
448 # FIXME: this might require using the charset of the mime part.
449 plaintext
= ptxt_fp
.getvalue().decode('utf-8')
451 sig_fingerprints
= []
453 sig_fingerprints
+= [res_
.fpr
]
455 return plaintext
, sig_fingerprints
458 def verify_detached_signature (detached_sig
, plaintext_bytes
, gpgme_ctx
):
460 detached_sig_fp
= io
.BytesIO(detached_sig
.encode('ascii'))
461 plaintext_fp
= io
.BytesIO(plaintext_bytes
)
462 ptxt_fp
= io
.BytesIO()
464 result
= gpgme_ctx
.verify(detached_sig_fp
, plaintext_fp
, None)
466 sig_fingerprints
= []
468 sig_fingerprints
+= [res_
.fpr
]
470 return sig_fingerprints
473 def decrypt_block (msg_block
, gpgme_ctx
):
475 block_b
= io
.BytesIO(msg_block
.encode('ascii'))
476 plain_b
= io
.BytesIO()
479 sigs
= gpgme_ctx
.decrypt_verify(block_b
, plain_b
)
483 plaintext
= plain_b
.getvalue().decode('utf-8')
484 return (plaintext
, sigs
)
487 def choose_reply_encryption_key (gpgme_ctx
, fingerprints
):
490 for fp
in fingerprints
:
492 key
= gpgme_ctx
.get_key(fp
)
494 if (key
.can_encrypt
== True):
504 def email_to_from_subject (email_text
):
506 email_struct
= email
.parser
.Parser().parsestr(email_text
)
508 email_to
= email_struct
['To']
509 email_from
= email_struct
['From']
510 email_subject
= email_struct
['Subject']
512 return email_to
, email_from
, email_subject
515 def import_lang(email_to
):
519 if "edward-" + lang
in email_to
:
520 lang
= "lang." + re
.sub('-', '_', lang
)
521 language
= importlib
.import_module(lang
)
525 return importlib
.import_module("lang.en")
528 def generate_encrypted_mime (plaintext
, email_from
, email_subject
, encrypt_to_key
,
532 reply
= "To: " + email_from
+ "\n"
533 reply
+= "Subject: " + email_subject
+ "\n"
535 if (encrypt_to_key
!= None):
536 plaintext_reply
= "thanks for the message!\n\n\n"
537 plaintext_reply
+= email_quote_text(plaintext
)
539 # quoted printable encoding lets most ascii characters look normal
540 # before the decrypted mime message is decoded.
541 char_set
= email
.charset
.Charset("utf-8")
542 char_set
.body_encoding
= email
.charset
.QP
544 # MIMEText doesn't allow setting the text encoding
545 # so we use MIMENonMultipart.
546 plaintext_mime
= MIMENonMultipart('text', 'plain')
547 plaintext_mime
.set_payload(plaintext_reply
, charset
=char_set
)
549 encrypted_text
= encrypt_sign_message(plaintext_mime
.as_string(),
553 control_mime
= MIMEApplication("Version: 1",
554 _subtype
='pgp-encrypted',
555 _encoder
=email
.encoders
.encode_7or8bit
)
556 control_mime
['Content-Description'] = 'PGP/MIME version identification'
557 control_mime
.set_charset('us-ascii')
559 encoded_mime
= MIMEApplication(encrypted_text
,
560 _subtype
='octet-stream; name="encrypted.asc"',
561 _encoder
=email
.encoders
.encode_7or8bit
)
562 encoded_mime
['Content-Description'] = 'OpenPGP encrypted message'
563 encoded_mime
['Content-Disposition'] = 'inline; filename="encrypted.asc"'
564 encoded_mime
.set_charset('us-ascii')
566 message_mime
= MIMEMultipart(_subtype
="encrypted", protocol
="application/pgp-encrypted")
567 message_mime
.attach(control_mime
)
568 message_mime
.attach(encoded_mime
)
569 message_mime
['Content-Disposition'] = 'inline'
571 reply
+= message_mime
.as_string()
575 reply
+= "Sorry, i couldn't find your key.\n"
576 reply
+= "I'll need that to encrypt a message to you."
581 def email_quote_text (text
):
583 quoted_message
= re
.sub(r
'^', r
'> ', text
, flags
=re
.MULTILINE
)
585 return quoted_message
588 def encrypt_sign_message (plaintext
, encrypt_to_key
, gpgme_ctx
):
590 plaintext_bytes
= io
.BytesIO(plaintext
.encode('ascii'))
591 encrypted_bytes
= io
.BytesIO()
593 gpgme_ctx
.encrypt_sign([encrypt_to_key
], gpgme
.ENCRYPT_ALWAYS_TRUST
,
594 plaintext_bytes
, encrypted_bytes
)
596 encrypted_txt
= encrypted_bytes
.getvalue().decode('ascii')
600 def error (error_msg
):
602 sys
.stderr
.write(progname
+ ": " + str(error_msg
) + "\n")
605 def debug (debug_msg
):
607 if edward_config
.debug
== True:
612 if __name__
== "__main__":
615 progname
= sys
.argv
[0]
617 if len(sys
.argv
) > 1:
618 print(progname
+ ": error, this program doesn't " \
619 "need any arguments.", file=sys
.stderr
)