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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
|
# 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/>.
"""The key email command."""
import copy
import logging
from email.mime.text import MIMEText
from mailman.email.message import UserNotification
from mailman.interfaces.command import ContinueProcessing, IEmailCommand
from mailman.interfaces.member import MemberRole
from mailman.interfaces.pending import IPendings
from mailman.interfaces.subscriptions import ISubscriptionManager
from mailman.interfaces.usermanager import IUserManager
from pgpy.constants import KeyFlags
from pgpy.errors import PGPError
from public import public
from zope.component import getUtility
from zope.interface import implementer
from mailman_pgp.config import mm_config
from mailman_pgp.database import transaction
from mailman_pgp.model.address import PGPAddress
from mailman_pgp.model.list import PGPMailingList
from mailman_pgp.pgp.mime import MIMEWrapper
from mailman_pgp.pgp.wrapper import PGPWrapper
from mailman_pgp.utils.email import get_email
from mailman_pgp.utils.pgp import key_merge, key_usable
from mailman_pgp.workflows.key_change import (CHANGE_CONFIRM_REQUEST,
KeyChangeModWorkflow,
KeyChangeWorkflow)
from mailman_pgp.workflows.key_confirm import CONFIRM_REQUEST
from mailman_pgp.workflows.key_revoke import KeyRevokeWorkflow
log = logging.getLogger('mailman.plugin.pgp.commands')
def _cmd_set(pgp_list, mlist, msg, msgdata, arguments, results):
"""
`key set "token"` command.
Used during the subscription to a PGP enabled mailing list, if the user
didn't already setup a `PGPaddress`.
* This command message CAN be encrypted to the list key, in which case it
will be decrypted.
* This command message MUST have exactly one transferrable PGP public key
attached (either PGP/MIME or inline PGP).
"""
if len(arguments) != 2:
print('Missing token.', file=results)
return ContinueProcessing.no
wrapped = PGPWrapper(msg)
if wrapped.is_encrypted():
decrypted = wrapped.try_decrypt(pgp_list.key)
wrapped = PGPWrapper(decrypted)
if not wrapped.has_keys():
print('No keys attached? Send a key.', file=results)
return ContinueProcessing.no
keys = list(wrapped.keys())
if len(keys) != 1:
print('More than one key! Send only one key.', file=results)
return ContinueProcessing.no
key = keys.pop()
if not key.is_public:
print('You probably wanted to send your public key only.',
file=results)
return ContinueProcessing.no
if not key_usable(key, {KeyFlags.EncryptCommunications, KeyFlags.Sign}):
print('Need a key which can be used to encrypt communications.',
file=results)
return ContinueProcessing.no
email = get_email(msg)
if not email:
print('No email to subscribe with.', file=results)
return ContinueProcessing.no
address = getUtility(IUserManager).get_address(email)
if not address:
print('No adddress to subscribe with.', file=results)
return ContinueProcessing.no
pgp_address = PGPAddress.for_address(address)
if pgp_address is None:
print('A pgp enabled address not found.', file=results)
return ContinueProcessing.no
token = arguments[1]
pendable = getUtility(IPendings).confirm(token, expunge=False)
if pendable is None:
print('Wrong token.', file=results)
return ContinueProcessing.no
with transaction():
pgp_address.key = key
ISubscriptionManager(mlist).confirm(token)
print('Key succesfully set.', file=results)
print('Key fingerprint: {}'.format(pgp_address.key.fingerprint),
file=results)
return ContinueProcessing.no
def _cmd_confirm(pgp_list, mlist, msg, msgdata, arguments, results):
"""
`key confirm "token"` command.
Used during subscription to confirm the setting of a users key, also for
confirming the change of a key of a subscriber after the `key change`
command.
* This command message CAN be encrypted to the list key, in which case it
will be decrypted.
* This command message MUST contain the appropriate statement of key
ownership, sent to the user after the `key set` or `key change` commands.
This statement MUST be singed by the users current key.
"""
if len(arguments) != 2:
print('Missing token.', file=results)
return ContinueProcessing.no
email = get_email(msg)
if not email:
print('No email to subscribe with.', file=results)
return ContinueProcessing.no
pgp_address = PGPAddress.for_email(email)
if pgp_address is None:
print('A pgp enabled address not found.', file=results)
return ContinueProcessing.no
if pgp_address.key is None:
print('No key set.', file=results)
return ContinueProcessing.no
wrapped = PGPWrapper(msg)
if wrapped.is_encrypted():
decrypted = wrapped.try_decrypt(pgp_list.key)
wrapped = PGPWrapper(decrypted)
if not wrapped.is_signed():
print('Message not signed, ignoring.', file=results)
return ContinueProcessing.no
if not wrapped.verifies(pgp_address.key):
print('Message failed to verify.', file=results)
return ContinueProcessing.no
token = arguments[1]
pendable = getUtility(IPendings).confirm(token, expunge=False)
if pendable is None:
print('Wrong token.', file=results)
return ContinueProcessing.no
if pendable.get('type') in (KeyChangeWorkflow.name,
KeyChangeModWorkflow.name):
expecting = CHANGE_CONFIRM_REQUEST.format(pendable.get('fingerprint'),
token)
else:
expecting = CONFIRM_REQUEST.format(pgp_address.key_fingerprint, token)
for sig_subject in wrapped.get_signed():
if expecting in sig_subject:
with transaction():
pgp_address.key_confirmed = True
ISubscriptionManager(mlist).confirm(token)
break
else:
print("Message doesn't contain the expected statement.", file=results)
return ContinueProcessing.no
def _cmd_change(pgp_list, mlist, msg, msgdata, arguments, results):
"""
`key change` command.
Used when a user wants to change a key of a `PGPAddress`.
* This command message CAN be encrypted to the list key, in which case it
will be decrypted.
* This command message MUST have exactly one transferrable PGP public key
attached (either PGP/MIME or inline PGP).
"""
if len(arguments) != 1:
print('Extraneous argument/s: ' + ','.join(arguments[1:]),
file=results)
return ContinueProcessing.no
email = get_email(msg)
if not email:
print('No email to change key of.', file=results)
return ContinueProcessing.no
pgp_address = PGPAddress.for_email(email)
if pgp_address is None:
print('A pgp enabled address not found.', file=results)
return ContinueProcessing.no
if pgp_address.key is None:
print("You currently don't have a key set.", file=results)
return ContinueProcessing.no
if not pgp_address.key_confirmed:
print('Your key is currently not confirmed.', file=results)
return ContinueProcessing.no
wrapped = PGPWrapper(msg)
if wrapped.is_encrypted():
decrypted = wrapped.try_decrypt(pgp_list.key)
wrapped = PGPWrapper(decrypted)
if not wrapped.has_keys():
print('No keys attached? Send a key.', file=results)
return ContinueProcessing.no
keys = list(wrapped.keys())
if len(keys) != 1:
print('More than one key! Send only one key.', file=results)
return ContinueProcessing.no
key = keys.pop()
if not key.is_public:
print('You probably wanted to send your public key only.',
file=results)
return ContinueProcessing.no
if not key_usable(key, {KeyFlags.EncryptCommunications, KeyFlags.Sign}):
print('Need a key which can be used to encrypt communications.',
file=results)
return ContinueProcessing.no
workflow_class = mm_config.workflows[pgp_list.key_change_workflow]
workflow = workflow_class(mlist, pgp_address, key)
list(workflow)
print('Key change request received.', file=results)
return ContinueProcessing.no
def _cmd_revoke(pgp_list, mlist, msg, msgdata, arguments, results):
"""
`key revoke` command.
Used when a user has to revoke a part of a key used for a `PGPAddress`.
* This command message CAN be encrypted to the list key, in which case it
will be decrypted.
* This command message MUST have at least one revocation certificate
attached.
"""
if len(arguments) != 1:
print('Extraneous argument/s: ' + ','.join(arguments[1:]),
file=results)
return ContinueProcessing.no
email = get_email(msg)
if not email:
print('No email to revoke key of.', file=results)
return ContinueProcessing.no
pgp_address = PGPAddress.for_email(email)
if pgp_address is None:
print('A pgp enabled address not found.', file=results)
return ContinueProcessing.no
key = pgp_address.key
if key is None:
print("You currently don't have a key set.", file=results)
return ContinueProcessing.no
if not pgp_address.key_confirmed:
print('Your key is currently not confirmed.', file=results)
return ContinueProcessing.no
wrapped = PGPWrapper(msg)
if wrapped.is_encrypted():
decrypted = wrapped.try_decrypt(pgp_list.key)
wrapped = PGPWrapper(decrypted)
if not wrapped.has_revocs():
print('No key revocations attached? Send a key revocation.',
file=results)
return ContinueProcessing.no
key_copy = copy.copy(key)
revocs = list(wrapped.revocs())
matches = 0
for revoc in revocs:
old_matches = matches
try:
verified = key_copy.verify(key_copy, revoc)
if verified:
key_copy |= revoc
matches += 1
continue
except PGPError:
pass
for subkey in key_copy.subkeys.values():
try:
verified = key_copy.verify(subkey, revoc)
if verified:
subkey |= revoc
matches += 1
break
except PGPError:
pass
# No match?
if matches == old_matches:
print('Revocation found for not-found key.', file=results)
if not key_usable(key_copy,
{KeyFlags.EncryptCommunications, KeyFlags.Sign}):
# Start reset process.
with transaction():
pgp_address.key = None
pgp_address.key_confirmed = False
workflow = KeyRevokeWorkflow(mlist, pgp_address)
list(workflow)
print('Key needs to be reset.', file=results)
else:
# Just update key.
if matches > 0:
with transaction():
pgp_address.key = key_copy
print('Key succesfully updated.', file=results)
else:
print('Nothing to do.', file=results)
return ContinueProcessing.yes
def _cmd_sign(pgp_list, mlist, msg, msgdata, arguments, results):
# List public key attached, signed by the users current key.
if len(arguments) != 1:
print('Extraneous argument/s: ' + ','.join(arguments[1:]),
file=results)
return ContinueProcessing.no
email = get_email(msg)
if not email:
print('No email.', file=results)
return ContinueProcessing.no
pgp_address = PGPAddress.for_email(email)
if pgp_address is None:
print('A pgp enabled address not found.', file=results)
return ContinueProcessing.no
key = pgp_address.key
if key is None:
print("You currently don't have a key set.", file=results)
return ContinueProcessing.no
if not pgp_address.key_confirmed:
print('Your key is currently not confirmed.', file=results)
return ContinueProcessing.no
wrapped = PGPWrapper(msg)
if wrapped.is_encrypted():
decrypted = wrapped.try_decrypt(pgp_list.key)
wrapped = PGPWrapper(decrypted)
if not wrapped.has_keys():
print('No keys attached? Send a key.', file=results)
return ContinueProcessing.no
keys = list(wrapped.keys())
if len(keys) != 1:
print('More than one key! Send only one key.', file=results)
return ContinueProcessing.no
key = keys.pop()
allowed_signers = pgp_list.key_signing_allowed
roster_map = {
MemberRole.member: mlist.members,
MemberRole.owner: mlist.owners,
MemberRole.moderator: mlist.moderators,
MemberRole.nonmember: mlist.nonmembers
}
allowed = False
for allowed_role in allowed_signers:
allowed_roster = roster_map[allowed_role]
if allowed_roster.get_member(email) is not None:
allowed = True
break
if not allowed:
print('You are not allowed to sign the list key.', file=results)
return ContinueProcessing.no
try:
key_merge(pgp_list.key, key, pgp_address.key)
except ValueError as e:
print(str(e), file=results)
return ContinueProcessing.no
pgp_list.fs_key.save()
print('List key updated with new signatures.', file=results)
return ContinueProcessing.yes
def _cmd_receive(pgp_list, mlist, msg, msgdata, arguments, results):
if len(arguments) != 1:
print('Extraneous argument/s: ' + ','.join(arguments[1:]),
file=results)
return ContinueProcessing.no
email = get_email(msg)
if not email:
print('No email to send list public key.', file=results)
return ContinueProcessing.no
msg = UserNotification(email, mlist.request_address,
'{} public key'.format(mlist.fqdn_listname))
msg.set_type('multipart/mixed')
msg['MIME-Version'] = '1.0'
msg.attach(MIMEText('Here is the public key you requested.'))
wrapped = MIMEWrapper(msg)
msg = wrapped.attach_keys(pgp_list.pubkey)
msg.send(mlist)
return ContinueProcessing.yes
SUBCOMMANDS = {
'set': _cmd_set,
'confirm': _cmd_confirm,
'change': _cmd_change,
'revoke': _cmd_revoke,
'sign': _cmd_sign,
'receive': _cmd_receive
}
ARGUMENTS = '<' + '|'.join(SUBCOMMANDS) + '>'
@public
@implementer(IEmailCommand)
class KeyCommand:
"""The `key` command."""
name = 'key'
argument_description = ARGUMENTS
short_description = 'PGP key management.'
description = """\
A general command for PGP key management for PGP enabled mailing lists.
`key set <token>`
A command used to set the address public key during subscription to a
mailing list. Is only required on the first subscription of a given
address to a PGP enabled mailing list. This command requires the command
message to have exactly one PGP public key attached (either PGP/MIME or
inline). This command should be encrypted to the mailing list public key.
`key confirm <token>`
A command used to confirm the setting of a new public key, either during
subscription or later after a `key change` command. Is only required on
the first subscription of a given address to a PGP enabled mailing list.
This command requires the command message to contain the statement sent
from the mailing list in response to the `key set <token>` command, and
requires this statement signed by the key that was attached to the `key
set` command message.
`key change`
A command used to change the address public key.
`key revoke`
A command used to revoke a part of/the whole address public key, when the
user has access to a revocation certificate (but not the signing
capability of the key to use `key change`). A revocation certificate
must be attached to the message like a PGP key would. The revocation
signature is verified, and the proper revocation is performed, if the
key is left unusable after that, you will be prompted to set and confirm
a new one.
`key sign`
A command used to add your signature to the list key, if you trust it. It
requires exactly one PGP public key attached, which should be the list
public key, signed with your address key. It is scanned for all new
certifications by your address key and the valid ones are imported. A
list might be configured to allow even a non-subscriber to sign its key.
`key receive`
A command used to request the list public key. The list public key will
be sent in a response.
"""
def process(self, mlist, msg, msgdata, arguments, results):
"""See `IEmailCommand`."""
if len(arguments) == 0:
print('No sub-command specified,'
' must be one of {}.'.format(ARGUMENTS), file=results)
return ContinueProcessing.no
if arguments[0] not in SUBCOMMANDS:
print('Wrong sub-command specified,'
' must be one of {}.'.format(ARGUMENTS), file=results)
return ContinueProcessing.no
pgp_list = PGPMailingList.for_list(mlist)
if pgp_list is None:
print("This mailing list doesn't have pgp enabled.", file=results)
return ContinueProcessing.no
command = SUBCOMMANDS[arguments[0]]
log.debug('key {}'.format(arguments[0]))
return command(pgp_list, mlist, msg, msgdata, arguments, results)
|