summaryrefslogtreecommitdiff
path: root/memoryhole/protection.py
blob: 900b29acc2a28904cdd233685737ff4cf72e1439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from email.mime.application import MIMEApplication

from memoryhole.gpg import Gnupg
from memoryhole.rfc3156 import PGPEncrypted, MultipartEncrypted


def protect(msg, openpgp=Gnupg(), encrypt=True, obscure=True):
    if encrypt:
        return _encrypt_mime(msg, openpgp)

    raise NotImplementedError()


def _encrypt_mime(msg, openpgp):
    newmsg = MultipartEncrypted('application/pgp-encrypted')
    for hkey, hval in msg.items():
        newmsg.add_header(hkey, hval)
        del(msg[hkey])

    encraddr = ""  # TODO
    signaddr = ""  # TODO
    encstr = openpgp.encrypt(msg.as_string(unixfrom=False),
                             encraddr, signaddr)
    encmsg = MIMEApplication(
        encstr, _subtype='octet-stream', _encoder=lambda x: x)
    encmsg.add_header('content-disposition', 'attachment',
                      filename='msg.asc')

    # create meta message
    metamsg = PGPEncrypted()
    metamsg.add_header('Content-Disposition', 'attachment')
    # attach pgp message parts to new message
    newmsg.attach(metamsg)
    newmsg.attach(encmsg)
    return newmsg