/
facility.py
347 lines (287 loc) · 11.8 KB
/
facility.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
##############################################################################
#
# Copyright (c) 2008 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Implementation of Key Management Facility
"""
from __future__ import absolute_import
import Crypto.Cipher
import Crypto.Cipher.PKCS1_v1_5
import Crypto.PublicKey.RSA
from Crypto.Random import random
import binascii
try:
# Python 3
from http.client import HTTPSConnection
from urllib.parse import urlparse
except ImportError:
# Python 2
from httplib import HTTPSConnection
from urlparse import urlparse
import logging
import os
import struct
import time
from zope.interface import implementer
from keas.kmi import interfaces
from hashlib import md5
__docformat__ = "reStructuredText"
logger = logging.getLogger('kmi')
@implementer(interfaces.IEncryptionService)
class EncryptionService(object):
CipherFactory = Crypto.Cipher.AES
CipherMode = Crypto.Cipher.AES.MODE_CBC
# Note: Decryption fails if you use an empty initialization vector; but it
# only fails when you restart the Python process. The length of the
# initialization vector is assumed to be 16 bytes because that's what
# openssl aes-256-cbc -nosalt -P -k 'a'
# prints if you execute it on the command line
initializationVector = '0123456789ABCDEF'
def _pkcs7Encode(self, text, k=16):
n = k - (len(text) % k)
return text + binascii.unhexlify(n * ("%02x" % n))
def _pkcs7Decode(self, text, k=16):
# In Python 3, text[-1] returns an int, not bytes, we need text[-1:] to
# have bytes. In Python 2, it doesn't matter, both return str.
# Actually it seems we could just do `n = text[-1]` in Python 3.
n = int(binascii.hexlify(text[-1:]), 16)
if n > k:
raise ValueError("Input is not padded or padding is corrupt")
return text[:-n]
_bytesToKeySalt = b'12345678'
def _bytesToKey(self, data):
# Simplified version of M2Crypto.m2.bytes_to_key().
assert len(self._bytesToKeySalt) == 8, len(self._bytesToKeySalt)
data += self._bytesToKeySalt
key = md5(data).digest()
key += md5(key + data).digest()
return key
def encrypt(self, key, data):
"""See interfaces.IEncryptionService"""
# 1. Extract the encryption key
encryptionKey = self._bytesToKey(self.getEncryptionKey(key))
# 2. Create a cipher object
cipher = self.CipherFactory.new(
key=encryptionKey, mode=self.CipherMode,
IV=self.initializationVector)
# 3. Apply padding.
data = self._pkcs7Encode(data)
# 4. Encrypt the data and return it.
return cipher.encrypt(data)
def encrypt_file(self, key, fsrc, fdst, chunksize=24*1024):
""" Encrypts a file with the given key.
:param key: The encryption key.
:param fsrc: File descriptor to read from (opened with 'rb')
:param fdst: File descriptor to write to (opened with 'wb').
Its an append operation.
:param chunksize: Sets the size of the chunk which the function
uses to read and encrypt the file. Larger chunk
sizes can be faster for some files and machines.
chunksize must be divisible by 16.
"""
# 1. Extract the encryption key
encryptionKey = self._bytesToKey(self.getEncryptionKey(key))
# 2. Create a random initialization vector
# bytes(bytearray(generator)) is needed for Python 2,
# with Python 3 bytes(generator) works
iv = bytes(bytearray((random.randint(0, 0xFF)) for i in range(16)))
# 3. Create a cipher object
cipher = self.CipherFactory.new(
key=encryptionKey, mode=self.CipherMode,
IV=iv
)
# 4. Get the current position so we can seek later back to it
# so we can write the filesize.
fdst_startpos = fdst.tell()
# 5. Write a spacer for the later filesize.
fdst.write(struct.pack('<Q', 0))
# 6. Write the initialization vector.
fdst.write(iv)
# 7. Read plain and write the encrypted file.
filesize = 0
while True:
chunk = fsrc.read(chunksize)
if len(chunk) == 0:
break
filesize += len(chunk)
# Apply padding.
if len(chunk) % 16 != 0:
chunk += b' ' * (16 - len(chunk) % 16)
# Write the chunk
fdst.write(cipher.encrypt(chunk))
# 8. Write the correct filesize.
fdst_endpos = fdst.tell()
fdst.seek(fdst_startpos)
fdst.write(struct.pack('<Q', filesize))
# 9. Seek back to end of the file
fdst.seek(fdst_endpos)
def decrypt(self, key, data):
"""See interfaces.IEncryptionService
:raises ValueError: if it can't decrypt the data.
"""
# 1. Extract the encryption key
encryptionKey = self._bytesToKey(self.getEncryptionKey(key))
# 2. Create a cipher object
cipher = self.CipherFactory.new(
key=encryptionKey, mode=self.CipherMode,
IV=self.initializationVector)
# 3. Decrypt the data.
text = cipher.decrypt(data)
# 4. Remove padding and return result.
return self._pkcs7Decode(text)
def decrypt_file(self, key, fsrc, fdst, chunksize=24*1024):
""" Decrypts a file using with the given key.
Parameters are similar to encrypt_file.
:raises ValueError: if it can't decrypt the file.
"""
origsize = struct.unpack('<Q', fsrc.read(struct.calcsize('Q')))[0]
iv = fsrc.read(16)
# 1. Extract the encryption key
encryptionKey = self._bytesToKey(self.getEncryptionKey(key))
# 2. Create a cipher object
cipher = self.CipherFactory.new(
key=encryptionKey, mode=self.CipherMode,
IV=iv
)
while True:
chunk = fsrc.read(chunksize)
if len(chunk) == 0:
break
fdst.write(cipher.decrypt(chunk))
fdst.truncate(origsize)
@implementer(interfaces.IExtendedKeyManagementFacility)
class KeyManagementFacility(EncryptionService):
timeout = 3600
rsaKeyLength = 2048 # The length of the key encrypting key
rsaKeyExponent = 65537 # Should be sufficiently high and non-symmetric
rsaPassphrase = 'key management facility'
keyLength = rsaKeyLength // 16
def __init__(self, storage_dir):
self.storage_dir = storage_dir
self.__data_cache = {}
self.__dek_cache = {}
def keys(self):
return [filename[:-4] for filename in os.listdir(self.storage_dir)
if filename.endswith('.dek')]
def __iter__(self):
return iter(self.keys())
def __getitem__(self, name):
if name in self.__data_cache:
return self.__data_cache[name]
if name+'.dek' not in os.listdir(self.storage_dir):
raise KeyError(name)
fn = os.path.join(self.storage_dir, name+'.dek')
with open(fn, 'rb') as file:
data = file.read()
self.__data_cache[name] = data
return data
def get(self, name, default=None):
try:
return self[name]
except KeyError:
return default
def values(self):
return [value for name, value in self.items()]
def __len__(self):
return len(self.keys())
def items(self):
return [(name, self[name]) for name in self.keys()]
def __contains__(self, name):
return name in self.keys()
has_key = __contains__
def __setitem__(self, name, key):
fn = os.path.join(self.storage_dir, name+'.dek')
with open(fn, 'wb') as file:
file.write(key)
logger.info('New key added (hash): %s', name)
def __delitem__(self, name):
if name in self.__data_cache:
del self.__data_cache[name]
fn = os.path.join(self.storage_dir, name+'.dek')
os.remove(fn)
logger.info('Key removed (hash): %s', name)
def generate(self):
"""See interfaces.IKeyGenerationService"""
# 1. Generate the private/public RSA key encrypting key
rsa = Crypto.PublicKey.RSA.generate(
self.rsaKeyLength, e=self.rsaKeyExponent)
# 2. Extract the private key from the RSA object
privateKey = rsa.exportKey(passphrase=self.rsaPassphrase)
# 3. Generate the encryption key
key = Crypto.Random.new().read(self.keyLength)
# 4. Create the lookup key in the container
hash = md5()
hash.update(privateKey)
# 5. Save the encryption key
encryptedKey = Crypto.Cipher.PKCS1_v1_5.new(rsa).encrypt(key)
self[hash.hexdigest()] = encryptedKey
# 6. Return the private key encrypting key
return privateKey
def getEncryptionKey(self, key):
"""Given the key encrypting key, get the encryption key."""
# 1. Create the lookup key in the container
hash = md5()
hash.update(key)
hash_key = hash.hexdigest()
# 2. Try to look up the key in the cache first.
if (hash_key in self.__dek_cache and
self.__dek_cache[hash_key][0] + self.timeout > time.time()):
return self.__dek_cache[hash_key][1]
# 3. Extract the encrypted encryption key
encryptedKey = self[hash_key]
# 4. Decrypt the key.
rsa = Crypto.PublicKey.RSA.importKey(key, self.rsaPassphrase)
error = object()
decryptedKey = Crypto.Cipher.PKCS1_v1_5.new(rsa).decrypt(
encryptedKey, error)
if decryptedKey is error:
raise ValueError('Error while decrypting key.')
# 5. Return decrypted encryption key
logger.info('Encryption key requested: %s', hash_key)
# 6. Add the key to the cache
self.__dek_cache[hash_key] = (time.time(), decryptedKey)
# 7. Return the key
return decryptedKey
def __repr__(self):
return '<%s (%i)>' %(self.__class__.__name__, len(self))
@implementer(interfaces.IKeyManagementFacility)
class LocalKeyManagementFacility(EncryptionService):
"""A local facility that requests keys from the master facility."""
timeout = 3600
httpConnFactory = HTTPSConnection
def __init__(self, url):
self.url = url
self.__cache = {}
def generate(self):
"""See interfaces.IKeyGenerationService"""
pieces = urlparse(self.url)
conn = self.httpConnFactory(pieces.netloc)
conn.request('POST', '/new', b'', {})
response = conn.getresponse()
data = response.read()
response.close()
return data
def getEncryptionKey(self, key):
"""Given the key encrypting key, get the encryption key."""
if (key in self.__cache and
self.__cache[key][0] + self.timeout > time.time()):
return self.__cache[key][1]
pieces = urlparse(self.url)
conn = self.httpConnFactory(pieces.netloc)
conn.request('POST', '/key', key, {'content-type': 'text/plain'})
response = conn.getresponse()
encryptionKey = response.read()
response.close()
self.__cache[key] = (time.time(), encryptionKey)
return encryptionKey
def __repr__(self):
return '<%s %r>' %(self.__class__.__name__, self.url)