key.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. from binascii import hexlify, a2b_base64, b2a_base64
  2. from getpass import getpass
  3. import os
  4. import msgpack
  5. import textwrap
  6. import hmac
  7. from hashlib import sha256
  8. import zlib
  9. try:
  10. import lzma # python >= 3.3
  11. except ImportError:
  12. try:
  13. from backports import lzma # backports.lzma from pypi
  14. except ImportError:
  15. lzma = None
  16. from attic.crypto import pbkdf2_sha256, get_random_bytes, AES, bytes_to_long, long_to_bytes, bytes_to_int, num_aes_blocks
  17. from attic.helpers import IntegrityError, get_keys_dir, Error
  18. PREFIX = b'\0' * 8
  19. class UnsupportedPayloadError(Error):
  20. """Unsupported payload type {}. A newer version is required to access this repository.
  21. """
  22. class HMAC(hmac.HMAC):
  23. """Workaround a bug in Python < 3.4 Where HMAC does not accept memoryviews
  24. """
  25. def update(self, msg):
  26. self.inner.update(msg)
  27. def key_creator(repository, args):
  28. if args.encryption == 'keyfile':
  29. return KeyfileKey.create(repository, args)
  30. if args.encryption == 'passphrase':
  31. return PassphraseKey.create(repository, args)
  32. if args.encryption == 'none':
  33. return PlaintextKey.create(repository, args)
  34. raise NotImplemented(args.encryption)
  35. def key_factory(repository, manifest_data):
  36. # key type is determined by 4 lower bits of the type byte
  37. key_type = manifest_data[0] & 0x0f
  38. if key_type == KeyfileKey.TYPE:
  39. return KeyfileKey.detect(repository, manifest_data)
  40. if key_type == PassphraseKey.TYPE:
  41. return PassphraseKey.detect(repository, manifest_data)
  42. if key_type == PlaintextKey.TYPE:
  43. return PlaintextKey.detect(repository, manifest_data)
  44. raise UnsupportedPayloadError(manifest_data[0])
  45. class CompressorBase(object):
  46. @classmethod
  47. def create(cls, args):
  48. return cls()
  49. def compress(self, data):
  50. pass
  51. def decompress(self, data):
  52. pass
  53. class ZlibCompressor(CompressorBase):
  54. TYPE = 0x00 # must be 0x00 for backwards compatibility
  55. def compress(self, data):
  56. return zlib.compress(data)
  57. def decompress(self, data):
  58. return zlib.decompress(data)
  59. class LzmaCompressor(CompressorBase):
  60. TYPE = 0x10
  61. def __init__(self):
  62. if lzma is None:
  63. raise NotImplemented("lzma compression needs Python >= 3.3 or backports.lzma from PyPi")
  64. def compress(self, data):
  65. return lzma.compress(data)
  66. def decompress(self, data):
  67. return lzma.decompress(data)
  68. def compressor_creator(args):
  69. if args is None: # used by unit tests
  70. return ZlibCompressor.create(args)
  71. if args.compression == 'lzma':
  72. return LzmaCompressor.create(args)
  73. if args.compression == 'zlib':
  74. return ZlibCompressor.create(args)
  75. raise NotImplemented(args.compression)
  76. def compressor_factory(manifest_data):
  77. # compression is determined by 4 upper bits of the type byte
  78. compression_type = manifest_data[0] & 0xf0
  79. if compression_type == ZlibCompressor.TYPE:
  80. return ZlibCompressor()
  81. if compression_type == LzmaCompressor.TYPE:
  82. return LzmaCompressor()
  83. raise UnsupportedPayloadError(manifest_data[0])
  84. class KeyBase(object):
  85. def __init__(self, compressor):
  86. self.compressor = compressor
  87. self.TYPE_STR = bytes([self.TYPE | self.compressor.TYPE])
  88. def id_hash(self, data):
  89. """Return HMAC hash using the "id" HMAC key
  90. """
  91. def encrypt(self, data):
  92. pass
  93. def decrypt(self, id, data):
  94. pass
  95. class PlaintextKey(KeyBase):
  96. TYPE = 0x02
  97. chunk_seed = 0
  98. @classmethod
  99. def create(cls, repository, args):
  100. print('Encryption NOT enabled.\nUse the "--encryption=passphrase|keyfile" to enable encryption.')
  101. compressor = compressor_creator(args)
  102. return cls(compressor)
  103. @classmethod
  104. def detect(cls, repository, manifest_data):
  105. compressor = compressor_factory(manifest_data)
  106. return cls(compressor)
  107. def id_hash(self, data):
  108. return sha256(data).digest()
  109. def encrypt(self, data):
  110. return b''.join([self.TYPE_STR, self.compressor.compress(data)])
  111. def decrypt(self, id, data):
  112. type_str = bytes([data[0]])
  113. if type_str != self.TYPE_STR:
  114. raise IntegrityError('Invalid encryption envelope %r' % type_str)
  115. data = self.compressor.decompress(memoryview(data)[1:])
  116. if id and sha256(data).digest() != id:
  117. raise IntegrityError('Chunk id verification failed')
  118. return data
  119. class AESKeyBase(KeyBase):
  120. """Common base class shared by KeyfileKey and PassphraseKey
  121. Chunks are encrypted using 256bit AES in Counter Mode (CTR)
  122. Payload layout: TYPE(1) + HMAC(32) + NONCE(8) + CIPHERTEXT
  123. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  124. in the payload, the first 8 bytes are always zeros. This does not
  125. affect security but limits the maximum repository capacity to
  126. only 295 exabytes!
  127. """
  128. PAYLOAD_OVERHEAD = 1 + 32 + 8 # TYPE + HMAC + NONCE
  129. def id_hash(self, data):
  130. """Return HMAC hash using the "id" HMAC key
  131. """
  132. return HMAC(self.id_key, data, sha256).digest()
  133. def encrypt(self, data):
  134. data = self.compressor.compress(data)
  135. self.enc_cipher.reset()
  136. data = b''.join((self.enc_cipher.iv[8:], self.enc_cipher.encrypt(data)))
  137. hmac = HMAC(self.enc_hmac_key, data, sha256).digest()
  138. return b''.join((self.TYPE_STR, hmac, data))
  139. def decrypt(self, id, data):
  140. if data[0] != self.TYPE:
  141. raise IntegrityError('Invalid encryption envelope')
  142. hmac = memoryview(data)[1:33]
  143. if memoryview(HMAC(self.enc_hmac_key, memoryview(data)[33:], sha256).digest()) != hmac:
  144. raise IntegrityError('Encryption envelope checksum mismatch')
  145. self.dec_cipher.reset(iv=PREFIX + data[33:41])
  146. data = self.compressor.decompress(self.dec_cipher.decrypt(data[41:])) # should use memoryview
  147. if id and HMAC(self.id_key, data, sha256).digest() != id:
  148. raise IntegrityError('Chunk id verification failed')
  149. return data
  150. def extract_nonce(self, payload):
  151. if payload[0] != self.TYPE:
  152. raise IntegrityError('Invalid encryption envelope')
  153. nonce = bytes_to_long(payload[33:41])
  154. return nonce
  155. def init_from_random_data(self, data):
  156. self.enc_key = data[0:32]
  157. self.enc_hmac_key = data[32:64]
  158. self.id_key = data[64:96]
  159. self.chunk_seed = bytes_to_int(data[96:100])
  160. # Convert to signed int32
  161. if self.chunk_seed & 0x80000000:
  162. self.chunk_seed = self.chunk_seed - 0xffffffff - 1
  163. def init_ciphers(self, enc_iv=b''):
  164. self.enc_cipher = AES(self.enc_key, enc_iv)
  165. self.dec_cipher = AES(self.enc_key)
  166. class PassphraseKey(AESKeyBase):
  167. TYPE = 0x01
  168. iterations = 100000
  169. @classmethod
  170. def create(cls, repository, args):
  171. compressor = compressor_creator(args)
  172. key = cls(compressor)
  173. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  174. if passphrase is not None:
  175. passphrase2 = passphrase
  176. else:
  177. passphrase, passphrase2 = 1, 2
  178. while passphrase != passphrase2:
  179. passphrase = getpass('Enter passphrase: ')
  180. if not passphrase:
  181. print('Passphrase must not be blank')
  182. continue
  183. passphrase2 = getpass('Enter same passphrase again: ')
  184. if passphrase != passphrase2:
  185. print('Passphrases do not match')
  186. key.init(repository, passphrase)
  187. if passphrase:
  188. print('Remember your passphrase. Your data will be inaccessible without it.')
  189. return key
  190. @classmethod
  191. def detect(cls, repository, manifest_data):
  192. prompt = 'Enter passphrase for %s: ' % repository._location.orig
  193. compressor = compressor_factory(manifest_data)
  194. key = cls(compressor)
  195. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  196. if passphrase is None:
  197. passphrase = getpass(prompt)
  198. while True:
  199. key.init(repository, passphrase)
  200. try:
  201. key.decrypt(None, manifest_data)
  202. num_blocks = num_aes_blocks(len(manifest_data) - 41)
  203. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  204. return key
  205. except IntegrityError:
  206. passphrase = getpass(prompt)
  207. def init(self, repository, passphrase):
  208. self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
  209. self.init_ciphers()
  210. class KeyfileKey(AESKeyBase):
  211. FILE_ID = 'ATTIC KEY'
  212. TYPE = 0x00
  213. @classmethod
  214. def detect(cls, repository, manifest_data):
  215. compressor = compressor_factory(manifest_data)
  216. key = cls(compressor)
  217. path = cls.find_key_file(repository)
  218. prompt = 'Enter passphrase for key file %s: ' % path
  219. passphrase = os.environ.get('ATTIC_PASSPHRASE', '')
  220. while not key.load(path, passphrase):
  221. passphrase = getpass(prompt)
  222. num_blocks = num_aes_blocks(len(manifest_data) - 41)
  223. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  224. return key
  225. @classmethod
  226. def find_key_file(cls, repository):
  227. id = hexlify(repository.id).decode('ascii')
  228. keys_dir = get_keys_dir()
  229. for name in os.listdir(keys_dir):
  230. filename = os.path.join(keys_dir, name)
  231. with open(filename, 'r') as fd:
  232. line = fd.readline().strip()
  233. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  234. return filename
  235. raise Exception('Key file for repository with ID %s not found' % id)
  236. def load(self, filename, passphrase):
  237. with open(filename, 'r') as fd:
  238. cdata = a2b_base64(''.join(fd.readlines()[1:]).encode('ascii')) # .encode needed for Python 3.[0-2]
  239. data = self.decrypt_key_file(cdata, passphrase)
  240. if data:
  241. key = msgpack.unpackb(data)
  242. if key[b'version'] != 1:
  243. raise IntegrityError('Invalid key file header')
  244. self.repository_id = key[b'repository_id']
  245. self.enc_key = key[b'enc_key']
  246. self.enc_hmac_key = key[b'enc_hmac_key']
  247. self.id_key = key[b'id_key']
  248. self.chunk_seed = key[b'chunk_seed']
  249. self.path = filename
  250. return True
  251. def decrypt_key_file(self, data, passphrase):
  252. d = msgpack.unpackb(data)
  253. assert d[b'version'] == 1
  254. assert d[b'algorithm'] == b'sha256'
  255. key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
  256. data = AES(key).decrypt(d[b'data'])
  257. if HMAC(key, data, sha256).digest() != d[b'hash']:
  258. return None
  259. return data
  260. def encrypt_key_file(self, data, passphrase):
  261. salt = get_random_bytes(32)
  262. iterations = 100000
  263. key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
  264. hash = HMAC(key, data, sha256).digest()
  265. cdata = AES(key).encrypt(data)
  266. d = {
  267. 'version': 1,
  268. 'salt': salt,
  269. 'iterations': iterations,
  270. 'algorithm': 'sha256',
  271. 'hash': hash,
  272. 'data': cdata,
  273. }
  274. return msgpack.packb(d)
  275. def save(self, path, passphrase):
  276. key = {
  277. 'version': 1,
  278. 'repository_id': self.repository_id,
  279. 'enc_key': self.enc_key,
  280. 'enc_hmac_key': self.enc_hmac_key,
  281. 'id_key': self.id_key,
  282. 'chunk_seed': self.chunk_seed,
  283. }
  284. data = self.encrypt_key_file(msgpack.packb(key), passphrase)
  285. with open(path, 'w') as fd:
  286. fd.write('%s %s\n' % (self.FILE_ID, hexlify(self.repository_id).decode('ascii')))
  287. fd.write('\n'.join(textwrap.wrap(b2a_base64(data).decode('ascii'))))
  288. fd.write('\n')
  289. self.path = path
  290. def change_passphrase(self):
  291. passphrase, passphrase2 = 1, 2
  292. while passphrase != passphrase2:
  293. passphrase = getpass('New passphrase: ')
  294. passphrase2 = getpass('Enter same passphrase again: ')
  295. if passphrase != passphrase2:
  296. print('Passphrases do not match')
  297. self.save(self.path, passphrase)
  298. print('Key file "%s" updated' % self.path)
  299. @classmethod
  300. def create(cls, repository, args):
  301. filename = args.repository.to_key_filename()
  302. path = filename
  303. i = 1
  304. while os.path.exists(path):
  305. i += 1
  306. path = filename + '.%d' % i
  307. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  308. if passphrase is not None:
  309. passphrase2 = passphrase
  310. else:
  311. passphrase, passphrase2 = 1, 2
  312. while passphrase != passphrase2:
  313. passphrase = getpass('Enter passphrase (empty for no passphrase):')
  314. passphrase2 = getpass('Enter same passphrase again: ')
  315. if passphrase != passphrase2:
  316. print('Passphrases do not match')
  317. compressor = compressor_creator(args)
  318. key = cls(compressor)
  319. key.repository_id = repository.id
  320. key.init_from_random_data(get_random_bytes(100))
  321. key.init_ciphers()
  322. key.save(path, passphrase)
  323. print('Key file "%s" created.' % key.path)
  324. print('Keep this file safe. Your data will be inaccessible without it.')
  325. return key