Handle PGP signed messages

This commit is contained in:
Peter J. Holzer 2019-03-01 13:54:13 +01:00
parent 02368a57f8
commit ea60f484a3
1 changed files with 25 additions and 0 deletions

View File

@ -7,7 +7,9 @@ import html.parser
import mailbox
import os
import re
import subprocess
import sys
import tempfile
import urllib.parse
import jinja2
@ -142,6 +144,29 @@ def render_body(msg):
}
bodyhtml = bodytmpl.render(context)
elif content_type == "multipart/signed":
content, signature = msg.get_payload()
with tempfile.NamedTemporaryFile(buffering=0) as content_fh:
content_fh.write(content.as_bytes())
with tempfile.NamedTemporaryFile(buffering=0, suffix=".asc") as signature_fh:
signature_fh.write(signature.get_payload(decode=True))
r = subprocess.run(["gpg", "--verify", signature_fh.name, content_fh.name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True)
gpgresult = r.stderr
# Analyze gpgresult or just use r,returncode?
gpgstatus = "dubious"
contenthtml = render_message(content)
bodytmpl = jenv.get_template("body_multipart_signed.html")
context = {
"content": contenthtml,
"gpgresult": gpgresult,
"gpgstatus": gpgstatus,
}
bodyhtml = bodytmpl.render(context)
else:
raise RuntimeError("Content-type " + content_type + " not implemented yet")