aboutsummaryrefslogtreecommitdiff
path: root/src/mailman_pgp/rules/tests/test_signature.py
blob: c6bcb1602c5b47ce30d62f6dd0a507fed4355482 (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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# 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 unittest import TestCase

from mailman.app.lifecycle import create_list
from mailman.interfaces.action import Action
from mailman.interfaces.chain import AcceptEvent
from mailman.interfaces.member import MemberRole
from mailman.interfaces.usermanager import IUserManager
from mailman.testing.helpers import (set_preferred,
                                     specialized_message_from_string as mfs)
from zope.component import getUtility
from zope.event import notify

from mailman_pgp.chains.default import PGPChain
from mailman_pgp.config import mm_config
from mailman_pgp.database import mm_transaction, transaction
from mailman_pgp.model.address import PGPAddress
from mailman_pgp.model.list import PGPMailingList
from mailman_pgp.model.sighash import PGPSigHash
from mailman_pgp.pgp.wrapper import PGPWrapper
from mailman_pgp.rules.signature import Signature
from mailman_pgp.testing.layers import PGPConfigLayer
from mailman_pgp.testing.pgp import load_key, load_message
from mailman_pgp.utils.pgp import hashes


class TestPGPSignatureRule(TestCase):
    layer = PGPConfigLayer

    def setUp(self):
        self.rule = Signature()

        user_manager = getUtility(IUserManager)
        with mm_transaction():
            self.mlist = create_list('test@example.com',
                                     style_name='pgp-default')
            self.sender = user_manager.create_user('RSA-1024b@example.org')
            set_preferred(self.sender)
            self.mlist.subscribe(self.sender, MemberRole.member)

        self.pgp_list = PGPMailingList.for_list(self.mlist)

        self.sender_key = load_key('rsa_1024.priv.asc')
        with transaction() as t:
            self.pgp_sender = PGPAddress(self.sender.preferred_address)
            self.pgp_sender.key = self.sender_key.pubkey
            self.pgp_sender.key_confirmed = True
            t.add(self.pgp_sender)

        self.msg_clear = load_message('clear.eml')
        self.msg_inline_signed = load_message('inline_signed.eml')
        self.msg_mime_signed = load_message('mime_signed.eml')
        self.msg_inline_signed_invalid = load_message(
                'inline_cleartext_signed_invalid.eml')
        self.msg_mime_signed_invalid = load_message(
                'mime_signed_invalid.eml')

    def assertAction(self, msgdata, action, reasons):
        self.assertEqual(msgdata['moderation_action'], action.name)
        self.assertListEqual(msgdata['moderation_reasons'], reasons)

    def test_has_rule(self):
        self.assertIn(Signature.name, mm_config.rules.keys())

    def test_no_pgp_list(self):
        with mm_transaction():
            ordinary_list = create_list('odrinary@example.com')
        msg = mfs("""\
From: anne@example.com
To: ordinary@example.com

""")

        matches = self.rule.check(ordinary_list, msg, {})
        self.assertFalse(matches)

    def test_no_address(self):
        with transaction():
            self.pgp_list.unsigned_msg_action = Action.defer
        msg = mfs("""\
From: anne@example.com
To: test@example.com

""")
        matches = self.rule.check(self.mlist, msg, {})
        self.assertFalse(matches)

    def test_no_key(self):
        with transaction():
            self.pgp_sender.key = None

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_mime_signed, msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.reject, [
            'No key set for address {}.'.format(
                    self.pgp_sender.address.original_email)])

    def test_key_not_confirmed(self):
        with transaction():
            self.pgp_sender.key_confirmed = False

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_mime_signed, msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.reject, ['Key not confirmed.'])

    def test_unsigned_action(self):
        with transaction():
            self.pgp_list.unsigned_msg_action = Action.hold
            self.pgp_list.inline_pgp_action = Action.defer
            self.pgp_list.expired_sig_action = Action.defer
            self.pgp_list.invalid_sig_action = Action.defer
            self.pgp_list.revoked_sig_action = Action.defer
            self.pgp_list.duplicate_sig_action = Action.defer

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_clear, msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.hold, ['The message is unsigned.'])

        matches = self.rule.check(self.mlist, self.msg_inline_signed, msgdata)
        self.assertFalse(matches)

        matches = self.rule.check(self.mlist, self.msg_mime_signed, msgdata)
        self.assertFalse(matches)

    def test_inline_pgp_action(self):
        with transaction():
            self.pgp_list.unsigned_msg_action = Action.defer
            self.pgp_list.inline_pgp_action = Action.hold
            self.pgp_list.expired_sig_action = Action.defer
            self.pgp_list.invalid_sig_action = Action.defer
            self.pgp_list.revoked_sig_action = Action.defer
            self.pgp_list.duplicate_sig_action = Action.defer

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_inline_signed, msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.hold, ['Inline PGP is not allowed.'])

        matches = self.rule.check(self.mlist, self.msg_mime_signed, msgdata)
        self.assertFalse(matches)

    def test_invalid_sig_action(self):
        with transaction():
            self.pgp_list.unsigned_msg_action = Action.defer
            self.pgp_list.inline_pgp_action = Action.defer
            self.pgp_list.expired_sig_action = Action.defer
            self.pgp_list.invalid_sig_action = Action.hold
            self.pgp_list.revoked_sig_action = Action.defer
            self.pgp_list.duplicate_sig_action = Action.defer

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_inline_signed_invalid,
                                  msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.hold, ['Signature did not verify.'])

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_mime_signed_invalid,
                                  msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.hold, ['Signature did not verify.'])

    def test_duplicate_sig_action(self):
        with transaction() as t:
            self.pgp_list.unsigned_msg_action = Action.defer
            self.pgp_list.inline_pgp_action = Action.defer
            self.pgp_list.expired_sig_action = Action.defer
            self.pgp_list.invalid_sig_action = Action.defer
            self.pgp_list.revoked_sig_action = Action.defer
            self.pgp_list.duplicate_sig_action = Action.hold

            wrapped = PGPWrapper(self.msg_mime_signed)
            sig_hashes = set(hashes(wrapped.verify(self.sender_key.pubkey)))
            wrapped = PGPWrapper(self.msg_inline_signed)
            sig_hashes |= set(hashes(wrapped.verify(self.sender_key.pubkey)))
            for hash in sig_hashes:
                sig_hash = PGPSigHash()
                sig_hash.hash = hash
                sig_hash.fingerprint = self.sender_key.pubkey.fingerprint
                t.add(sig_hash)

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_mime_signed, msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.hold, ['Signature duplicate.'])

        msgdata = {}
        matches = self.rule.check(self.mlist, self.msg_inline_signed, msgdata)
        self.assertTrue(matches)
        self.assertAction(msgdata, Action.hold, ['Signature duplicate.'])


class TestPostingEvent(TestCase):
    layer = PGPConfigLayer

    def setUp(self):
        self.rule = Signature()

        user_manager = getUtility(IUserManager)
        with mm_transaction():
            self.mlist = create_list('test@example.com',
                                     style_name='pgp-default')
            self.sender = user_manager.create_user('RSA-1024b@example.org')
            set_preferred(self.sender)
            self.mlist.subscribe(self.sender, MemberRole.member)

        self.pgp_list = PGPMailingList.for_list(self.mlist)

        self.sender_key = load_key('rsa_1024.priv.asc')
        with transaction() as t:
            self.pgp_sender = PGPAddress(self.sender.preferred_address)
            self.pgp_sender.key = self.sender_key.pubkey
            self.pgp_sender.key_confirmed = True
            t.add(self.pgp_sender)

    def test_sighashes_added(self):
        msg = load_message('mime_signed.eml')
        wrapped = PGPWrapper(msg)
        sighashes = set(hashes(wrapped.verify(self.sender_key)))
        msgdata = dict(pgp_sig_hashes=sighashes)
        notify(AcceptEvent(self.mlist, msg, msgdata,
                           mm_config.chains[PGPChain.name]))

        for hash in sighashes:
            sig_hash = PGPSigHash.query().filter_by(hash=hash).one()
            self.assertIsNotNone(sig_hash)
            self.assertEqual(sig_hash.fingerprint, self.sender_key.fingerprint)