This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_keys.py
736 lines (560 loc) · 23.8 KB
/
_keys.py
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import os as _os
import base64 as _base64
import uuid as _uuid
from Acquire.Stubs import lazy_import as _lazy_import
_rsa = _lazy_import.lazy_module("cryptography.hazmat.primitives.asymmetric.rsa")
_serialization = _lazy_import.lazy_module("cryptography.hazmat.primitives.serialization")
_default_backend = _lazy_import.lazy_function("cryptography.hazmat.backends.default_backend")
_hashes = _lazy_import.lazy_module("cryptography.hazmat.primitives.hashes")
_padding = _lazy_import.lazy_module("cryptography.hazmat.primitives.asymmetric.padding")
_fernet = _lazy_import.lazy_module("cryptography.fernet")
__all__ = ["PrivateKey", "PublicKey", "SymmetricKey", "get_private_key"]
def _bytes_to_string(b):
"""Return the passed binary bytes safely encoded to
a base64 utf-8 string"""
if b is None:
return None
else:
return _base64.b64encode(b).decode("utf-8")
def _string_to_bytes(s):
"""Return the passed base64 utf-8 encoded binary data
back converted from a string back to bytes. Note that
this can only convert strings that were encoded using
bytes_to_string - you cannot use this to convert
arbitrary strings to bytes"""
if s is None:
return None
else:
return _base64.b64decode(s.encode("utf-8"))
def _assert_strong_passphrase(passphrase, mangleFunction):
"""This function returns whether or not the passed
passphrase is sufficiently strong. To be strong,
the password must be between 6-50 characters,
mix upper and lower case, and contain letters and
numbers
"""
if mangleFunction:
passphrase = str(mangleFunction(passphrase))
else:
passphrase = str(passphrase)
if len(passphrase) < 6 or len(passphrase) > 50:
from Acquire.Crypto import WeakPassphraseError
print(passphrase)
raise WeakPassphraseError(
"The pass-phrase '%s' must contain between " "6 and 50 characters" % passphrase
)
if len(passphrase) < 24:
import re as _re
if not (
_re.search(r"[A-Z]", passphrase)
and _re.search(r"[a-z]", passphrase)
and _re.search(r"[0-9]", passphrase)
):
from Acquire.Crypto import WeakPassphraseError
print(passphrase)
raise WeakPassphraseError(
"Short pass-phrases must contain numbers and " "upper- and lowercase characters"
)
# an MD5 passphrase is 32 characters, only lower case and numbers
return passphrase
def _generate_private_key():
"""Internal function that is used to generate all of our private keys"""
return _rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=_default_backend())
def _generate_symmetric_key():
"""Internal function that is used to generate the symmetric keys"""
return _fernet.Fernet.generate_key()
_key_database = {}
def get_private_key(key="default"):
"""Internal function used to return the key associated with
'key'. This allows a single session to re-use a key, rather
than continually generating new keys (which is slow). Make
sure you know that you can safely re-use a key. Re-using
keys for different function calls is completely ok
"""
global _key_database
if key in _key_database:
return _key_database[key]
else:
privkey = PrivateKey(name="global_key %s" % key)
_key_database[key] = privkey
return privkey
class PublicKey:
"""This is a holder for an in-memory public key"""
def __init__(self, public_key=None):
"""Construct from the passed public key"""
self._pubkey = public_key
def bytes(self):
"""Return the raw bytes for this key"""
if self._pubkey is None:
return None
return self._pubkey.public_bytes(
encoding=_serialization.Encoding.PEM, format=_serialization.PublicFormat.SubjectPublicKeyInfo
)
def pem(self):
"""Return a PEM string for this key"""
return self.bytes().decode("utf-8")
def __str__(self):
"""Return a string representation of this key"""
return "PublicKey('%s')" % self.bytes().decode("utf-8")
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.bytes() == other.bytes()
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
def write(self, filename):
"""Write this public key to 'filename'"""
if self._pubkey is None:
return
pubkey_bytes = self.bytes()
with open(filename, "wb") as FILE:
FILE.write(pubkey_bytes)
@staticmethod
def read_bytes(data):
"""Read and return a public key from 'data'"""
public_key = _serialization.load_pem_public_key(data, backend=_default_backend())
return PublicKey(public_key)
@staticmethod
def read(filename):
"""Read and return a public key from 'filename'"""
with open(filename, "rb") as FILE:
return PublicKey.read_bytes(FILE.read())
def fingerprint(self):
"""Return the fingerprint of this key - this is useful to help
work out which key to use to decrypt data
"""
from hashlib import md5 as _md5
md5 = _md5()
md5.update(self.bytes())
h = md5.hexdigest()
# return this signature as "AA:BB:CC:DD:EE:etc."
return ":".join([h[i : i + 2] for i in range(0, len(h), 2)])
def encrypt(self, message):
"""Encrypt and return the passed message. For short messages this
will use the private key directly. For longer messages,
this will generate a random
symmetric key, will encrypt the message using that, and will then
encrypt the symmetric key. This returns some bytes
"""
if isinstance(message, str):
message = message.encode("utf-8")
try:
return self._pubkey.encrypt(
message,
_padding.OAEP(
mgf=_padding.MGF1(algorithm=_hashes.SHA256()), algorithm=_hashes.SHA256(), label=None
),
)
except:
pass
# this is a longer message that cannot be encoded using
# an asymmetric key - need to use a symmetric key
key = _fernet.Fernet.generate_key()
f = _fernet.Fernet(key)
token = f.encrypt(message)
encrypted_key = self._pubkey.encrypt(
key,
_padding.OAEP(
mgf=_padding.MGF1(algorithm=_hashes.SHA256()), algorithm=_hashes.SHA256(), label=None
),
)
# the first 256 bytes are the encrypted key - the rest
# is the token, because we are using 2048 bit (256 byte) keys
return encrypted_key + token
def verify(self, signature, message):
"""Verify that the message has been correctly signed"""
if self._pubkey is None:
from Acquire.Crypto import KeyManipulationError
raise KeyManipulationError("You cannot verify a message using " "an empty public key!")
if isinstance(message, str):
message = message.encode("utf-8")
try:
self._pubkey.verify(
signature,
message,
_padding.PSS(mgf=_padding.MGF1(_hashes.SHA256()), salt_length=_padding.PSS.MAX_LENGTH),
_hashes.SHA256(),
)
except Exception as e:
from Acquire.Crypto import SignatureVerificationError
raise SignatureVerificationError(
"Error validating the signature " "for the passed message: %s" % str(e)
)
def to_data(self):
"""Return this public key as a json-serialisable dictionary"""
data = {}
b = self.bytes()
if b is not None:
data["bytes"] = _bytes_to_string(self.bytes())
return data
@staticmethod
def from_data(data):
"""Construct from the passed json-deserialised dictionary"""
if isinstance(data, str):
return PublicKey.read_bytes(_string_to_bytes(data))
elif isinstance(data, bytes):
return PublicKey.read_bytes(data)
else:
key = PublicKey()
if data and len(data) > 0:
key = PublicKey.read_bytes(_string_to_bytes(data["bytes"]))
return key
class PrivateKey:
"""This is a holder for an in-memory private key"""
def __init__(self, private_key=None, auto_generate=True, name=None):
"""Construct the key either from a passed key, or by generating
a new key"""
self._privkey = private_key
self._name = name
if self._privkey is None:
if auto_generate:
self._privkey = _generate_private_key()
def __str__(self):
"""Return a string representation of this key"""
return "PrivateKey( public_key='%s' )" % self.public_key().bytes().decode("utf-8")
def __eq__(self, other):
if isinstance(other, self.__class__):
return self.public_key() == other.public_key()
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
@staticmethod
def read_bytes(data, passphrase, mangleFunction=None):
"""Read a private key from the passed bytes 'data' that
is encrypted using 'passphrase' and return a PrivateKey
object holding that key
"""
passphrase = _assert_strong_passphrase(passphrase, mangleFunction)
private_key = None
try:
private_key = _serialization.load_pem_private_key(
data, password=passphrase.encode("utf-8"), backend=_default_backend()
)
except Exception as e:
from Acquire.Crypto import KeyManipulationError
raise KeyManipulationError("Cannot unlock key. %s" % str(e))
return PrivateKey(private_key)
@staticmethod
def read(filename, passphrase, mangleFunction=None):
"""Read a private key from 'filename' that is encrypted using
'passphrase' and return a PrivateKey object holding that
key"""
data = None
try:
with open(filename, "rb") as FILE:
data = FILE.read()
except IOError as e:
from Acquire.Crypto import KeyManipulationError
raise KeyManipulationError("Cannot read the private keyfile %s: %s" % (filename, str(e)))
return PrivateKey.read_bytes(data, passphrase, mangleFunction)
@staticmethod
def random_passphrase():
"""Randomly generate and return a passphrase that obeys the
password rules and could be used to serialise a PrivateKey
"""
import random as _random
import string as _string
# use the operating system as source of random numbers
rand = _random.SystemRandom()
# generate a random password comprised of a random set of
# upper, lower and digits characters
nvals = int(rand.uniform(20, 40))
nlower = int(rand.uniform(1, nvals - 5))
nupper = int(rand.uniform(1, nvals - nlower - 5))
ndigits = nvals - nupper - nlower
assert nlower > 0
assert nupper > 0
assert ndigits > 0
lower = [rand.choice(_string.ascii_lowercase) for _ in range(nlower)]
upper = [rand.choice(_string.ascii_uppercase) for _ in range(nupper)]
digits = [rand.choice(_string.digits) for _ in range(ndigits)]
passphrase = "".join(rand.sample(lower + upper + digits, nvals))
_assert_strong_passphrase(passphrase, mangleFunction=None)
return passphrase
def pem(self, passphrase, mangleFunction=None):
"""Return a PEM string for this key"""
return self.bytes(passphrase, mangleFunction).decode("utf-8")
def bytes(self, passphrase, mangleFunction=None):
"""Return the raw bytes for this key, encoded by the passed
passphrase that has been optionally mangled by mangleFunction"""
if self._privkey is None:
return None
passphrase = _assert_strong_passphrase(passphrase, mangleFunction)
return self._privkey.private_bytes(
encoding=_serialization.Encoding.PEM,
format=_serialization.PrivateFormat.PKCS8,
encryption_algorithm=_serialization.BestAvailableEncryption(passphrase.encode("utf-8")),
)
def write(self, filename, passphrase, mangleFunction=None):
"""Write this key to 'filename', encrypted with 'passphrase'"""
if self._privkey is None:
return
privkey_bytes = self.bytes(passphrase, mangleFunction)
with open(_os.open(filename, _os.O_CREAT | _os.O_WRONLY, 0o700), "wb") as FILE:
FILE.write(privkey_bytes)
def public_key(self, filename=None):
"""Get the public key for this private key. If filename is
specified then this is written to the passed file"""
if self._privkey is None:
return None
return PublicKey(self._privkey.public_key())
def key_size_in_bytes(self):
"""Return the number of bytes in this key"""
if self._privkey is None:
return 0
else:
return int(self._privkey.key_size / 8)
def fingerprint(self):
"""Return the fingerprint of this key - this is useful to help
work out which key to use to decrypt data
"""
return self.public_key().fingerprint()
def encrypt(self, message):
"""Encrypt and return the passed message"""
return self.public_key().encrypt(message)
def verify(self, signature, message):
"""Verify the passed signature is correct for the passed message"""
return self.public_key().verify(signature, message)
def decrypt(self, message):
"""Decrypt and return the passed message"""
key_size = self.key_size_in_bytes()
if key_size == 0:
from Acquire.Crypto import DecryptionError
raise DecryptionError("You cannot decrypt a message " "with a null key!")
# try standard decryption
try:
message = self._privkey.decrypt(
message,
_padding.OAEP(
mgf=_padding.MGF1(algorithm=_hashes.SHA256()), algorithm=_hashes.SHA256(), label=None
),
)
try:
return message.decode("utf-8")
except:
return message
except:
pass
# it is a larger message, so need to decrypt the secret symmetric
# key, and then use that to decrypt the rest of the token
try:
symkey = self._privkey.decrypt(
message[0:key_size],
_padding.OAEP(
mgf=_padding.MGF1(algorithm=_hashes.SHA256()), algorithm=_hashes.SHA256(), label=None
),
)
except Exception as e:
from Acquire.Crypto import DecryptionError
raise DecryptionError(
"Cannot decrypt the symmetric key used "
"to encrypt the long message '%s' (%s): %s" % (message[0:key_size], key_size, str(e))
)
try:
f = _fernet.Fernet(symkey)
except:
f = _fernet.Fernet(symkey.decode("utf-8"))
try:
try:
message = f.decrypt(message[key_size:])
except:
message = f.decrypt(message[key_size:].encode("utf-8"))
except Exception as e:
from Acquire.Crypto import DecryptionError
raise DecryptionError("Cannot decrypt the long message using the " "symmetric key: %s" % str(e))
try:
return message.decode("utf-8")
except:
return message
def sign(self, message):
"""Return the signature for the passed message"""
if self._privkey is None:
return None
if isinstance(message, str):
message = message.encode("utf-8")
signature = self._privkey.sign(
message,
_padding.PSS(mgf=_padding.MGF1(_hashes.SHA256()), salt_length=_padding.PSS.MAX_LENGTH),
_hashes.SHA256(),
)
return signature
def to_data(self, passphrase, mangleFunction=None):
"""Return the json-serialisable data for this key"""
data = {}
b = self.bytes(passphrase, mangleFunction)
if b is not None:
data["bytes"] = _bytes_to_string(b)
return data
@staticmethod
def from_data(data, passphrase, mangleFunction=None):
"""Return a private key constructed from the passed json-deserialised
dictionary
"""
if isinstance(data, str):
return PrivateKey.read_bytes(_string_to_bytes(data), passphrase, mangleFunction)
elif isinstance(data, bytes):
return PrivateKey.read_bytes(data, passphrase, mangleFunction)
elif data and len(data) > 0:
return PrivateKey.read_bytes(_string_to_bytes(data["bytes"]), passphrase, mangleFunction)
else:
return None
class SymmetricKey:
"""This is a holder for an in-memory symmetric key
(for symmetric encryption)
"""
def __init__(self, symmetric_key=None, auto_generate=True):
"""Construct the key either from a passed key, or by generating
a new key. The passed key will be converted into a
URL-safe base64-encoded 32byte key
"""
if symmetric_key is not None:
from Acquire.Crypto import Hash as _Hash
from Acquire.ObjectStore import string_to_encoded as _string_to_encoded
self._symkey = _string_to_encoded(_Hash.md5(symmetric_key)).encode("utf-8")
else:
if auto_generate:
self._symkey = _generate_symmetric_key()
def __str__(self):
"""Return a string representation of this key"""
return "SymmetricKey()"
def __eq__(self, other):
if isinstance(other, self.__class__):
return self._symkey == other._symkey
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
@staticmethod
def read_bytes(data, passphrase, mangleFunction=None):
"""Read a SymmetricKey from the passed bytes 'data' that
is encrypted using 'passphrase' and return a SymmetricKey
object holding that key
"""
passphrase = _assert_strong_passphrase(passphrase, mangleFunction)
symmetric_key = SymmetricKey(auto_generate=False)
try:
s = SymmetricKey(symmetric_key=passphrase)
symmetric_key._symkey = s.decrypt(data)
except Exception as e:
from Acquire.Crypto import KeyManipulationError
raise KeyManipulationError("Cannot unlock key. %s" % str(e))
try:
symmetric_key._symkey = symmetric_key._symkey.encode("utf-8")
except:
pass
return symmetric_key
@staticmethod
def read(filename, passphrase, mangleFunction=None):
"""Read a private key from 'filename' that is encrypted using
'passphrase' and return a SymmetricKey object holding that
key"""
data = None
try:
with open(filename, "rb") as FILE:
data = FILE.read()
except IOError as e:
from Acquire.Crypto import KeyManipulationError
raise KeyManipulationError("Cannot read the keyfile %s: %s" % (filename, str(e)))
return SymmetricKey.read_bytes(data, passphrase, mangleFunction)
@staticmethod
def random_passphrase():
"""Randomly generate and return a passphrase that obeys the
password rules and could be used to serialise a SymmetricKey
"""
import random as _random
import string as _string
# use the operating system as source of random numbers
rand = _random.SystemRandom()
# generate a random password comprised of a random set of
# upper, lower and digits characters
nvals = int(rand.uniform(20, 40))
nlower = int(rand.uniform(1, nvals - 5))
nupper = int(rand.uniform(1, nvals - nlower - 5))
ndigits = nvals - nupper - nlower
assert nlower > 0
assert nupper > 0
assert ndigits > 0
lower = [rand.choice(_string.ascii_lowercase) for _ in range(nlower)]
upper = [rand.choice(_string.ascii_uppercase) for _ in range(nupper)]
digits = [rand.choice(_string.digits) for _ in range(ndigits)]
passphrase = "".join(rand.sample(lower + upper + digits, nvals))
_assert_strong_passphrase(passphrase, mangleFunction=None)
return passphrase
def bytes(self, passphrase, mangleFunction=None):
"""Return the raw bytes for this key, encoded by the passed
passphrase that has been optionally mangled by mangleFunction"""
if self._symkey is None:
return None
passphrase = _assert_strong_passphrase(passphrase, mangleFunction)
s = SymmetricKey(symmetric_key=passphrase)
return s.encrypt(self._symkey)
def write(self, filename, passphrase, mangleFunction=None):
"""Write this key to 'filename', encrypted with 'passphrase'"""
if self._symkey is None:
return
symkey_bytes = self.bytes(passphrase, mangleFunction)
with open(_os.open(filename, _os.O_CREAT | _os.O_WRONLY, 0o700), "wb") as FILE:
FILE.write(symkey_bytes)
def fingerprint(self):
"""Return the fingerprint of this key - this is useful to help
work out which key to use to decrypt data
"""
if self._symkey is None:
return None
from hashlib import md5 as _md5
md5 = _md5()
md5.update(self._symkey)
h = md5.hexdigest()
# return this signature as "AA:BB:CC:DD:EE:etc."
return ":".join([h[i : i + 2] for i in range(0, len(h), 2)])
def encrypt(self, message):
"""Encrypt and return the passed message"""
if self._symkey is None:
self._symkey = _generate_symmetric_key()
if isinstance(message, str):
message = message.encode("utf-8")
assert type(self._symkey) is bytes
f = _fernet.Fernet(self._symkey)
token = f.encrypt(message)
return token
def decrypt(self, message):
"""Decrypt and return the passed message"""
if self._symkey is None:
from Acquire.Crypto import DecryptionError
raise DecryptionError("You cannot decrypt a message " "with a null key!")
assert type(self._symkey) is bytes
f = _fernet.Fernet(self._symkey)
try:
message = f.decrypt(message)
except Exception as e:
from Acquire.Crypto import DecryptionError
raise DecryptionError(
"Cannot decrypt the message using the "
"symmetric key: %s : %s : %s" % (str(e), self._symkey, message)
)
try:
return message.decode("utf-8")
except:
return message
def to_data(self, passphrase, mangleFunction=None):
"""Return the json-serialisable data for this key"""
data = {}
b = self.bytes(passphrase, mangleFunction)
if b is not None:
data["bytes"] = _bytes_to_string(b)
return data
@staticmethod
def from_data(data, passphrase, mangleFunction=None):
"""Return a key constructed from the passed json-deserialised
dictionary
"""
if isinstance(data, str):
return SymmetricKey.read_bytes(_string_to_bytes(data), passphrase, mangleFunction)
elif isinstance(data, bytes):
return SymmetricKey.read_bytes(data, passphrase, mangleFunction)
elif data and len(data) > 0:
return SymmetricKey.read_bytes(_string_to_bytes(data["bytes"]), passphrase, mangleFunction)
else:
return None