blob: 8b87abec8794f81deb7666f8d2f745549a550dd9 (
plain) (
blame)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# Copyright (C) 2017 Jan Jancar
#
# This file is a part of the Mailman PGP plugin.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
""""""
from email.message import Message
from email.utils import collapse_rfc2231_value
from pgpy import PGPKey, PGPSignature
class PGPMIMEWrapper:
_signed_subtype = 'application/pgp-signature'
_encrypted_subtype = 'application/pgp-encrypted'
def __init__(self, msg: Message):
self.msg = msg
def _is_mime(self):
is_multipart = self.msg.is_multipart()
payloads = len(self.msg.get_payload())
return is_multipart and payloads == 2
def is_mime_signed(self):
"""
Whether the whole message is MIME signed as per RFC3156 section 5.
:return:
"""
if not self._is_mime():
return False
second_type = self.msg.get_payload(1).get_content_type()
protocol_param = collapse_rfc2231_value(self.msg.get_param('protocol'))
content_subtype = self.msg.get_content_subtype()
return second_type == PGPMIMEWrapper._signed_subtype and \
content_subtype == 'signed' and \
protocol_param == PGPMIMEWrapper._signed_subtype
def is_mime_encrypted(self):
"""
Whether the whole message is MIME encrypted as per RFC3156 section 4.
:return:
"""
if not self._is_mime():
return False
first_part = str(self.msg.get_payload(0))
first_type = self.msg.get_payload(0).get_content_type()
second_type = self.msg.get_payload(1).get_content_type()
content_subtype = self.msg.get_content_subtype()
protocol_param = collapse_rfc2231_value(self.msg.get_param('protocol'))
return 'Version: 1' in first_part and \
first_type == PGPMIMEWrapper._encrypted_subtype and \
second_type == 'application/octet-stream' and \
content_subtype == 'encrypted' and \
protocol_param == PGPMIMEWrapper._encrypted_subtype
def verify(self, key: PGPKey):
"""
:param key:
:return:
"""
clear_text = str(self.msg.get_payload(0))
sig_text = self.msg.get_payload(1).get_payload()
signature = PGPSignature.from_blob(sig_text)
verification = key.verify(clear_text,
signature)
return signature in [v.signature for v in verification.good_signatures]
def decrypt(self, key: PGPKey):
pass
|