borg/darc/key.py

399 lines
14 KiB
Python
Raw Normal View History

from binascii import hexlify, unhexlify, a2b_base64, b2a_base64
from getpass import getpass
import os
import msgpack
import re
import shutil
import tempfile
2013-06-20 11:42:10 +00:00
import textwrap
import unittest
import hmac
from hashlib import sha256
import zlib
from .crypto import pbkdf2_sha256, get_random_bytes, AES, bytes_to_long, long_to_bytes, bytes_to_int
from .helpers import IntegrityError, get_keys_dir, Location
2013-06-03 11:45:48 +00:00
PREFIX = b'\0' * 8
2013-06-03 11:45:48 +00:00
KEYFILE = b'\0'
PASSPHRASE = b'\1'
PLAINTEXT = b'\2'
class HMAC(hmac.HMAC):
def update(self, msg):
self.inner.update(msg)
2013-06-20 10:44:58 +00:00
def key_creator(repository, args):
if args.keyfile:
2013-06-20 10:44:58 +00:00
return KeyfileKey.create(repository, args)
elif args.passphrase:
2013-06-20 10:44:58 +00:00
return PassphraseKey.create(repository, args)
else:
2013-06-20 10:44:58 +00:00
return PlaintextKey.create(repository, args)
2013-06-20 10:44:58 +00:00
def key_factory(repository, manifest_data):
2013-06-03 11:45:48 +00:00
if manifest_data[:1] == KEYFILE:
2013-06-20 10:44:58 +00:00
return KeyfileKey.detect(repository, manifest_data)
2013-06-03 11:45:48 +00:00
elif manifest_data[:1] == PASSPHRASE:
2013-06-20 10:44:58 +00:00
return PassphraseKey.detect(repository, manifest_data)
2013-06-03 11:45:48 +00:00
elif manifest_data[:1] == PLAINTEXT:
2013-06-20 10:44:58 +00:00
return PlaintextKey.detect(repository, manifest_data)
else:
raise Exception('Unkown Key type %d' % ord(manifest_data[0]))
2011-10-29 15:01:07 +00:00
class KeyBase(object):
def id_hash(self, data):
"""Return HMAC hash using the "id" HMAC key
"""
def encrypt(self, data):
pass
def decrypt(self, id, data):
pass
class PlaintextKey(KeyBase):
TYPE = PLAINTEXT
chunk_seed = 0
@classmethod
2013-06-20 10:44:58 +00:00
def create(cls, repository, args):
2013-06-03 11:45:48 +00:00
print('Encryption NOT enabled.\nUse the --key-file or --passphrase options to enable encryption.')
return cls()
@classmethod
2013-06-20 10:44:58 +00:00
def detect(cls, repository, manifest_data):
return cls()
def id_hash(self, data):
return sha256(data).digest()
def encrypt(self, data):
2013-06-03 11:45:48 +00:00
return b''.join([self.TYPE, zlib.compress(data)])
def decrypt(self, id, data):
2013-06-03 11:45:48 +00:00
if data[:1] != self.TYPE:
raise IntegrityError('Invalid encryption envelope')
2013-06-03 11:45:48 +00:00
data = zlib.decompress(memoryview(data)[1:])
if id and sha256(data).digest() != id:
raise IntegrityError('Chunk id verification failed')
return data
class AESKeyBase(KeyBase):
def id_hash(self, data):
"""Return HMAC hash using the "id" HMAC key
"""
return HMAC(self.id_key, data, sha256).digest()
def encrypt(self, data):
data = zlib.compress(data)
self.enc_cipher.reset()
data = b''.join((self.enc_cipher.iv[8:], self.enc_cipher.encrypt(data)))
hash = HMAC(self.enc_hmac_key, data, sha256).digest()
2013-06-03 11:45:48 +00:00
return b''.join((self.TYPE, hash, data))
def decrypt(self, id, data):
2013-06-03 11:45:48 +00:00
if data[:1] != self.TYPE:
raise IntegrityError('Invalid encryption envelope')
2013-06-03 11:45:48 +00:00
hash = memoryview(data)[1:33]
if memoryview(HMAC(self.enc_hmac_key, memoryview(data)[33:], sha256).digest()) != hash:
raise IntegrityError('Encryption envelope checksum mismatch')
self.dec_cipher.reset(iv=PREFIX + data[33:41])
data = zlib.decompress(self.dec_cipher.decrypt(data[41:])) # should use memoryview
if id and HMAC(self.id_key, data, sha256).digest() != id:
raise IntegrityError('Chunk id verification failed')
return data
2012-12-18 20:58:58 +00:00
def extract_iv(self, payload):
2013-06-03 11:45:48 +00:00
if payload[:1] != self.TYPE:
2012-12-18 20:58:58 +00:00
raise IntegrityError('Invalid encryption envelope')
nonce = bytes_to_long(payload[33:41])
return nonce
def init_from_random_data(self, data):
self.enc_key = data[0:32]
self.enc_hmac_key = data[32:64]
self.id_key = data[64:96]
self.chunk_seed = bytes_to_int(data[96:100])
# Convert to signed int32
if self.chunk_seed & 0x80000000:
self.chunk_seed = self.chunk_seed - 0xffffffff - 1
def init_ciphers(self, enc_iv=b''):
self.enc_cipher = AES(self.enc_key, enc_iv)
self.dec_cipher = AES(self.enc_key)
class PassphraseKey(AESKeyBase):
TYPE = PASSPHRASE
iterations = 100000
@classmethod
2013-06-20 10:44:58 +00:00
def create(cls, repository, args):
key = cls()
passphrase = os.environ.get('DARC_PASSPHRASE')
if passphrase is not None:
passphrase2 = passphrase
else:
passphrase, passphrase2 = 1, 2
while passphrase != passphrase2:
passphrase = getpass('Enter passphrase: ')
if not passphrase:
2013-06-03 11:45:48 +00:00
print('Passphrase must not be blank')
continue
passphrase2 = getpass('Enter same passphrase again: ')
if passphrase != passphrase2:
2013-06-03 11:45:48 +00:00
print('Passphrases do not match')
2013-06-20 10:44:58 +00:00
key.init(repository, passphrase)
if passphrase:
2013-06-03 11:45:48 +00:00
print('Remember your passphrase. Your data will be inaccessible without it.')
return key
@classmethod
2013-06-20 10:44:58 +00:00
def detect(cls, repository, manifest_data):
prompt = 'Enter passphrase for %s: ' % repository._location.orig
key = cls()
passphrase = os.environ.get('DARC_PASSPHRASE')
if passphrase is None:
passphrase = getpass(prompt)
while True:
2013-06-20 10:44:58 +00:00
key.init(repository, passphrase)
try:
key.decrypt(None, manifest_data)
key.init_ciphers(PREFIX + long_to_bytes(key.extract_iv(manifest_data) + 1000))
return key
except IntegrityError:
passphrase = getpass(prompt)
2013-06-20 10:44:58 +00:00
def init(self, repository, passphrase):
self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
self.init_ciphers()
class KeyfileKey(AESKeyBase):
FILE_ID = 'DARC KEY'
TYPE = KEYFILE
@classmethod
2013-06-20 10:44:58 +00:00
def detect(cls, repository, manifest_data):
key = cls()
2013-06-20 10:44:58 +00:00
path = cls.find_key_file(repository)
prompt = 'Enter passphrase for key file %s: ' % path
passphrase = os.environ.get('DARC_PASSPHRASE', '')
while not key.load(path, passphrase):
passphrase = getpass(prompt)
key.init_ciphers(PREFIX + long_to_bytes(key.extract_iv(manifest_data) + 1000))
return key
@classmethod
2013-06-20 10:44:58 +00:00
def find_key_file(cls, repository):
id = hexlify(repository.id).decode('ascii')
2011-08-06 11:01:58 +00:00
keys_dir = get_keys_dir()
2011-08-04 13:27:52 +00:00
for name in os.listdir(keys_dir):
filename = os.path.join(keys_dir, name)
2013-06-03 11:45:48 +00:00
with open(filename, 'r') as fd:
2011-08-04 13:27:52 +00:00
line = fd.readline().strip()
if line and line.startswith(cls.FILE_ID) and line[9:] == id:
2011-08-04 13:27:52 +00:00
return filename
2013-06-20 10:44:58 +00:00
raise Exception('Key file for repository with ID %s not found' % id)
2011-08-04 13:27:52 +00:00
def load(self, filename, passphrase):
2013-06-03 11:45:48 +00:00
with open(filename, 'r') as fd:
cdata = a2b_base64(''.join(fd.readlines()[1:]).encode('ascii')) # .encode needed for Python 3.[0-2]
data = self.decrypt_key_file(cdata, passphrase)
if data:
key = msgpack.unpackb(data)
2013-06-03 11:45:48 +00:00
if key[b'version'] != 1:
raise IntegrityError('Invalid key file header')
2013-06-20 10:44:58 +00:00
self.repository_id = key[b'repository_id']
2013-06-03 11:45:48 +00:00
self.enc_key = key[b'enc_key']
self.enc_hmac_key = key[b'enc_hmac_key']
self.id_key = key[b'id_key']
self.chunk_seed = key[b'chunk_seed']
self.path = filename
return True
def decrypt_key_file(self, data, passphrase):
d = msgpack.unpackb(data)
2013-06-03 11:45:48 +00:00
assert d[b'version'] == 1
2013-06-20 11:42:10 +00:00
assert d[b'algorithm'] == b'sha256'
key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
2013-06-20 11:42:10 +00:00
data = AES(key).decrypt(d[b'data'])
if HMAC(key, data, sha256).digest() != d[b'hash']:
return None
return data
def encrypt_key_file(self, data, passphrase):
salt = get_random_bytes(32)
2013-06-20 11:42:10 +00:00
iterations = 100000
key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
hash = HMAC(key, data, sha256).digest()
2013-06-20 11:42:10 +00:00
cdata = AES(key).encrypt(data)
d = {
'version': 1,
'salt': salt,
'iterations': iterations,
2013-06-20 11:42:10 +00:00
'algorithm': 'sha256',
'hash': hash,
'data': cdata,
}
return msgpack.packb(d)
def save(self, path, passphrase):
key = {
'version': 1,
2013-06-20 10:44:58 +00:00
'repository_id': self.repository_id,
'enc_key': self.enc_key,
'enc_hmac_key': self.enc_hmac_key,
2012-12-18 20:58:58 +00:00
'id_key': self.id_key,
'chunk_seed': self.chunk_seed,
}
data = self.encrypt_key_file(msgpack.packb(key), passphrase)
2013-06-03 11:45:48 +00:00
with open(path, 'w') as fd:
2013-06-20 10:44:58 +00:00
fd.write('%s %s\n' % (self.FILE_ID, hexlify(self.repository_id).decode('ascii')))
2013-06-20 11:42:10 +00:00
fd.write('\n'.join(textwrap.wrap(b2a_base64(data).decode('ascii'))))
2011-10-27 20:17:47 +00:00
self.path = path
def change_passphrase(self):
passphrase, passphrase2 = 1, 2
while passphrase != passphrase2:
passphrase = getpass('New passphrase: ')
passphrase2 = getpass('Enter same passphrase again: ')
if passphrase != passphrase2:
2013-06-03 11:45:48 +00:00
print('Passphrases do not match')
self.save(self.path, passphrase)
2013-06-03 11:45:48 +00:00
print('Key file "%s" updated' % self.path)
@classmethod
2013-06-20 10:44:58 +00:00
def create(cls, repository, args):
filename = args.repository.to_key_filename()
2011-08-04 13:27:52 +00:00
path = filename
i = 1
2011-08-04 13:27:52 +00:00
while os.path.exists(path):
i += 1
path = filename + '.%d' % i
passphrase = os.environ.get('DARC_PASSPHRASE')
if passphrase is not None:
passphrase2 = passphrase
2011-08-06 11:01:58 +00:00
else:
passphrase, passphrase2 = 1, 2
while passphrase != passphrase2:
passphrase = getpass('Enter passphrase (empty for no passphrase):')
passphrase2 = getpass('Enter same passphrase again: ')
if passphrase != passphrase2:
2013-06-03 11:45:48 +00:00
print('Passphrases do not match')
key = cls()
2013-06-20 10:44:58 +00:00
key.repository_id = repository.id
key.init_from_random_data(get_random_bytes(100))
key.init_ciphers()
key.save(path, passphrase)
2013-06-03 11:45:48 +00:00
print('Key file "%s" created.' % key.path)
print('Keep this file safe. Your data will be inaccessible without it.')
return key
class KeyTestCase(unittest.TestCase):
class MockArgs(object):
repository = Location(tempfile.mkstemp()[1])
keyfile2_key_file = """
DARC KEY 0000000000000000000000000000000000000000000000000000000000000000
hqppdGVyYXRpb25zzgABhqCkaGFzaNoAIMyonNI+7Cjv0qHi0AOBM6bLGxACJhfgzVD2oq
bIS9SFqWFsZ29yaXRobaZzaGEyNTakc2FsdNoAINNK5qqJc1JWSUjACwFEWGTdM7Nd0a5l
1uBGPEb+9XM9p3ZlcnNpb24BpGRhdGHaANAYDT5yfPpU099oBJwMomsxouKyx/OG4QIXK2
hQCG2L2L/9PUu4WIuKvGrsXoP7syemujNfcZws5jLp2UPva4PkQhQsrF1RYDEMLh2eF9Ol
rwtkThq1tnh7KjWMG9Ijt7/aoQtq0zDYP/xaFF8XXSJxiyP5zjH5+spB6RL0oQHvbsliSh
/cXJq7jrqmrJ1phd6dg4SHAM/i+hubadZoS6m25OQzYAW09wZD/phG8OVa698Z5ed3HTaT
SmrtgJL3EoOKgUI9d6BLE4dJdBqntifo""".strip()
keyfile2_cdata = unhexlify(re.sub('\W', '', """
0055f161493fcfc16276e8c31493c4641e1eb19a79d0326fad0291e5a9c98e5933
00000000000003e8d21eaf9b86c297a8cd56432e1915bb
"""))
keyfile2_id = unhexlify('c3fbf14bc001ebcc3cd86e696c13482ed071740927cd7cbe1b01b4bfcee49314')
2012-12-18 20:58:58 +00:00
def setUp(self):
self.tmppath = tempfile.mkdtemp()
os.environ['DARC_KEYS_DIR'] = self.tmppath
def tearDown(self):
shutil.rmtree(self.tmppath)
2013-06-20 10:44:58 +00:00
class MockRepository(object):
2012-12-18 20:58:58 +00:00
class _Location(object):
orig = '/some/place'
_location = _Location()
id = bytes(32)
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
2012-12-18 20:58:58 +00:00
os.environ['DARC_KEYS_DIR'] = self.tmpdir
def tearDown(self):
shutil.rmtree(self.tmpdir)
def test_plaintext(self):
key = PlaintextKey.create(None, None)
2013-06-03 11:45:48 +00:00
data = b'foo'
self.assertEqual(hexlify(key.id_hash(data)), b'2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae')
self.assertEqual(data, key.decrypt(key.id_hash(data), key.encrypt(data)))
def test_keyfile(self):
os.environ['DARC_PASSPHRASE'] = 'test'
key = KeyfileKey.create(self.MockRepository(), self.MockArgs())
self.assertEqual(bytes_to_long(key.enc_cipher.iv, 8), 0)
2013-06-03 11:45:48 +00:00
manifest = key.encrypt(b'')
2012-12-18 20:58:58 +00:00
iv = key.extract_iv(manifest)
2013-06-20 10:44:58 +00:00
key2 = KeyfileKey.detect(self.MockRepository(), manifest)
self.assertEqual(bytes_to_long(key2.enc_cipher.iv, 8), iv + 1000)
2012-12-18 20:58:58 +00:00
# Key data sanity check
self.assertEqual(len(set([key2.id_key, key2.enc_key, key2.enc_hmac_key])), 3)
self.assertEqual(key2.chunk_seed == 0, False)
2013-06-03 11:45:48 +00:00
data = b'foo'
2012-12-18 20:58:58 +00:00
self.assertEqual(data, key2.decrypt(key.id_hash(data), key.encrypt(data)))
def test_keyfile2(self):
with open(os.path.join(os.environ['DARC_KEYS_DIR'], 'keyfile'), 'w') as fd:
fd.write(self.keyfile2_key_file)
os.environ['DARC_PASSPHRASE'] = 'passphrase'
key = KeyfileKey.detect(self.MockRepository(), self.keyfile2_cdata)
self.assertEqual(key.decrypt(self.keyfile2_id, self.keyfile2_cdata), b'payload')
def test_passphrase(self):
os.environ['DARC_PASSPHRASE'] = 'test'
2013-06-20 10:44:58 +00:00
key = PassphraseKey.create(self.MockRepository(), None)
self.assertEqual(bytes_to_long(key.enc_cipher.iv, 8), 0)
self.assertEqual(hexlify(key.id_key), b'793b0717f9d8fb01c751a487e9b827897ceea62409870600013fbc6b4d8d7ca6')
self.assertEqual(hexlify(key.enc_hmac_key), b'b885a05d329a086627412a6142aaeb9f6c54ab7950f996dd65587251f6bc0901')
self.assertEqual(hexlify(key.enc_key), b'2ff3654c6daf7381dbbe718d2b20b4f1ea1e34caa6cc65f6bb3ac376b93fed2a')
self.assertEqual(key.chunk_seed, -775740477)
2013-06-03 11:45:48 +00:00
manifest = key.encrypt(b'')
2012-12-18 20:58:58 +00:00
iv = key.extract_iv(manifest)
2013-06-20 10:44:58 +00:00
key2 = PassphraseKey.detect(self.MockRepository(), manifest)
self.assertEqual(bytes_to_long(key2.enc_cipher.iv, 8), iv + 1000)
2012-12-18 20:58:58 +00:00
self.assertEqual(key.id_key, key2.id_key)
self.assertEqual(key.enc_hmac_key, key2.enc_hmac_key)
self.assertEqual(key.enc_key, key2.enc_key)
self.assertEqual(key.chunk_seed, key2.chunk_seed)
2013-06-03 11:45:48 +00:00
data = b'foo'
self.assertEqual(hexlify(key.id_hash(data)), b'818217cf07d37efad3860766dcdf1d21e401650fed2d76ed1d797d3aae925990')
2012-12-18 20:58:58 +00:00
self.assertEqual(data, key2.decrypt(key2.id_hash(data), key.encrypt(data)))
def suite():
return unittest.TestLoader().loadTestsFromTestCase(KeyTestCase)
if __name__ == '__main__':
unittest.main()