key.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. from binascii import hexlify, a2b_base64, b2a_base64
  2. from getpass import getpass
  3. import os
  4. import msgpack
  5. import textwrap
  6. from collections import namedtuple
  7. import hmac
  8. from hashlib import sha256, sha512
  9. import zlib
  10. try:
  11. import lzma # python >= 3.3
  12. except ImportError:
  13. try:
  14. from backports import lzma # backports.lzma from pypi
  15. except ImportError:
  16. lzma = None
  17. from attic.crypto import pbkdf2_sha256, get_random_bytes, AES, bytes_to_long, long_to_bytes, bytes_to_int, num_aes_blocks
  18. from attic.helpers import IntegrityError, get_keys_dir, Error
  19. # we do not store the full IV on disk, as the upper 8 bytes are expected to be
  20. # zero anyway as the full IV is a 128bit counter. PREFIX are the upper 8 bytes,
  21. # stored_iv are the lower 8 Bytes.
  22. PREFIX = b'\0' * 8
  23. Meta = namedtuple('Meta', 'compr_type, crypt_type, mac_type, hmac, stored_iv')
  24. class UnsupportedPayloadError(Error):
  25. """Unsupported payload type {}. A newer version is required to access this repository.
  26. """
  27. class sha512_256(object): # note: can't subclass sha512
  28. """sha512, but digest truncated to 256bit - faster than sha256 on 64bit platforms"""
  29. digestsize = digest_size = 32
  30. block_size = 64
  31. def __init__(self, data=None):
  32. self.name = 'sha512-256'
  33. self._h = sha512()
  34. if data:
  35. self.update(data)
  36. def update(self, data):
  37. self._h.update(data)
  38. def digest(self):
  39. return self._h.digest()[:self.digest_size]
  40. def hexdigest(self):
  41. return self._h.hexdigest()[:self.digest_size * 2]
  42. def copy(self):
  43. new = sha512_256.__new__(sha512_256)
  44. new._h = self._h.copy()
  45. return new
  46. class HMAC(hmac.HMAC):
  47. """Workaround a bug in Python < 3.4 Where HMAC does not accept memoryviews
  48. """
  49. def update(self, msg):
  50. self.inner.update(msg)
  51. class SHA256(object): # note: can't subclass sha256
  52. TYPE = 0
  53. def __init__(self, key, data=b''):
  54. # signature is like for a MAC, we ignore the key as this is a simple hash
  55. if key is not None:
  56. raise Exception("use a HMAC if you have a key")
  57. self.h = sha256(data)
  58. def update(self, data):
  59. self.h.update(data)
  60. def digest(self):
  61. return self.h.digest()
  62. def hexdigest(self):
  63. return self.h.hexdigest()
  64. class SHA512_256(sha512_256):
  65. """sha512, but digest truncated to 256bit - faster than sha256 on 64bit platforms"""
  66. TYPE = 1
  67. def __init__(self, key, data):
  68. # signature is like for a MAC, we ignore the key as this is a simple hash
  69. if key is not None:
  70. raise Exception("use a HMAC if you have a key")
  71. super().__init__(data)
  72. HASH_DEFAULT = SHA256.TYPE
  73. class HMAC_SHA256(HMAC):
  74. TYPE = 10
  75. def __init__(self, key, data):
  76. if key is None:
  77. raise Exception("do not use HMAC if you don't have a key")
  78. super().__init__(key, data, sha256)
  79. class HMAC_SHA512_256(HMAC):
  80. TYPE = 11
  81. def __init__(self, key, data):
  82. if key is None:
  83. raise Exception("do not use HMAC if you don't have a key")
  84. super().__init__(key, data, sha512_256)
  85. MAC_DEFAULT = HMAC_SHA256.TYPE
  86. class ZlibCompressor(object): # uses 0..9 in the mapping
  87. TYPE = 0
  88. LEVELS = range(10)
  89. def compress(self, data):
  90. level = self.TYPE - ZlibCompressor.TYPE
  91. return zlib.compress(data, level)
  92. def decompress(self, data):
  93. return zlib.decompress(data)
  94. class LzmaCompressor(object): # uses 10..19 in the mapping
  95. TYPE = 10
  96. PRESETS = range(10)
  97. def __init__(self):
  98. if lzma is None:
  99. raise NotImplemented("lzma compression needs Python >= 3.3 or backports.lzma from PyPi")
  100. def compress(self, data):
  101. preset = self.TYPE - LzmaCompressor.TYPE
  102. return lzma.compress(data, preset=preset)
  103. def decompress(self, data):
  104. return lzma.decompress(data)
  105. COMPR_DEFAULT = ZlibCompressor.TYPE + 6 # zlib level 6
  106. class KeyBase(object):
  107. TYPE = 0x00 # override in derived classes
  108. def __init__(self, compressor, maccer):
  109. self.compressor = compressor()
  110. self.maccer = maccer
  111. def id_hash(self, data):
  112. """Return HMAC hash using the "id" HMAC key
  113. """
  114. def encrypt(self, data):
  115. pass
  116. def decrypt(self, id, data):
  117. pass
  118. class PlaintextKey(KeyBase):
  119. TYPE = 0x02
  120. chunk_seed = 0
  121. @classmethod
  122. def create(cls, repository, args):
  123. print('Encryption NOT enabled.\nUse the "--encryption=passphrase|keyfile" to enable encryption.')
  124. compressor = compressor_creator(args)
  125. maccer = maccer_creator(args, cls)
  126. return cls(compressor, maccer)
  127. @classmethod
  128. def detect(cls, repository, manifest_data):
  129. meta, data, compressor, crypter, maccer = parser(manifest_data)
  130. return cls(compressor, maccer)
  131. def id_hash(self, data):
  132. return self.maccer(None, data).digest()
  133. def encrypt(self, data):
  134. meta = Meta(compr_type=self.compressor.TYPE, crypt_type=self.TYPE, mac_type=self.maccer.TYPE,
  135. hmac=None, stored_iv=None)
  136. data = self.compressor.compress(data)
  137. return generate(meta, data)
  138. def decrypt(self, id, data):
  139. meta, data, compressor, crypter, maccer = parser(data)
  140. assert isinstance(self, crypter)
  141. assert self.maccer is maccer
  142. data = self.compressor.decompress(data)
  143. if id and self.id_hash(data) != id:
  144. raise IntegrityError('Chunk id verification failed')
  145. return data
  146. class AESKeyBase(KeyBase):
  147. """Common base class shared by KeyfileKey and PassphraseKey
  148. Chunks are encrypted using 256bit AES in Counter Mode (CTR)
  149. """
  150. def id_hash(self, data):
  151. """Return HMAC hash using the "id" HMAC key
  152. """
  153. return self.maccer(self.id_key, data).digest()
  154. def encrypt(self, data):
  155. data = self.compressor.compress(data)
  156. self.enc_cipher.reset()
  157. stored_iv = self.enc_cipher.iv[8:]
  158. data = self.enc_cipher.encrypt(data)
  159. hmac = self.maccer(self.enc_hmac_key, stored_iv + data).digest()
  160. meta = Meta(compr_type=self.compressor.TYPE, crypt_type=self.TYPE, mac_type=self.maccer.TYPE,
  161. hmac=hmac, stored_iv=stored_iv)
  162. return generate(meta, data)
  163. def decrypt(self, id, data):
  164. meta, data, compressor, crypter, maccer = parser(data)
  165. assert isinstance(self, crypter)
  166. assert self.maccer is maccer
  167. computed_hmac = self.maccer(self.enc_hmac_key, meta.stored_iv + data).digest()
  168. if computed_hmac != meta.hmac:
  169. raise IntegrityError('Encryption envelope checksum mismatch')
  170. self.dec_cipher.reset(iv=PREFIX + meta.stored_iv)
  171. data = self.compressor.decompress(self.dec_cipher.decrypt(data))
  172. if id and self.id_hash(data) != id:
  173. raise IntegrityError('Chunk id verification failed')
  174. return data
  175. def extract_nonce(self, payload):
  176. meta, data, compressor, crypter, maccer = parser(payload)
  177. assert isinstance(self, crypter)
  178. nonce = bytes_to_long(meta.stored_iv)
  179. return nonce
  180. def init_from_random_data(self, data):
  181. self.enc_key = data[0:32]
  182. self.enc_hmac_key = data[32:64]
  183. self.id_key = data[64:96]
  184. self.chunk_seed = bytes_to_int(data[96:100])
  185. # Convert to signed int32
  186. if self.chunk_seed & 0x80000000:
  187. self.chunk_seed = self.chunk_seed - 0xffffffff - 1
  188. def init_ciphers(self, enc_iv=b''):
  189. self.enc_cipher = AES(self.enc_key, enc_iv)
  190. self.dec_cipher = AES(self.enc_key)
  191. class PassphraseKey(AESKeyBase):
  192. TYPE = 0x01
  193. iterations = 100000
  194. @classmethod
  195. def create(cls, repository, args):
  196. compressor = compressor_creator(args)
  197. maccer = maccer_creator(args, cls)
  198. key = cls(compressor, maccer)
  199. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  200. if passphrase is not None:
  201. passphrase2 = passphrase
  202. else:
  203. passphrase, passphrase2 = 1, 2
  204. while passphrase != passphrase2:
  205. passphrase = getpass('Enter passphrase: ')
  206. if not passphrase:
  207. print('Passphrase must not be blank')
  208. continue
  209. passphrase2 = getpass('Enter same passphrase again: ')
  210. if passphrase != passphrase2:
  211. print('Passphrases do not match')
  212. key.init(repository, passphrase)
  213. if passphrase:
  214. print('Remember your passphrase. Your data will be inaccessible without it.')
  215. return key
  216. @classmethod
  217. def detect(cls, repository, manifest_data):
  218. prompt = 'Enter passphrase for %s: ' % repository._location.orig
  219. meta, data, compressor, crypter, maccer = parser(manifest_data)
  220. key = cls(compressor, maccer)
  221. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  222. if passphrase is None:
  223. passphrase = getpass(prompt)
  224. while True:
  225. key.init(repository, passphrase)
  226. try:
  227. key.decrypt(None, manifest_data)
  228. num_blocks = num_aes_blocks(len(data))
  229. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  230. return key
  231. except IntegrityError:
  232. passphrase = getpass(prompt)
  233. def init(self, repository, passphrase):
  234. self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
  235. self.init_ciphers()
  236. class KeyfileKey(AESKeyBase):
  237. FILE_ID = 'ATTIC KEY'
  238. TYPE = 0x00
  239. @classmethod
  240. def detect(cls, repository, manifest_data):
  241. meta, data, compressor, crypter, maccer = parser(manifest_data)
  242. key = cls(compressor, maccer)
  243. path = cls.find_key_file(repository)
  244. prompt = 'Enter passphrase for key file %s: ' % path
  245. passphrase = os.environ.get('ATTIC_PASSPHRASE', '')
  246. while not key.load(path, passphrase):
  247. passphrase = getpass(prompt)
  248. num_blocks = num_aes_blocks(len(data))
  249. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  250. return key
  251. @classmethod
  252. def find_key_file(cls, repository):
  253. id = hexlify(repository.id).decode('ascii')
  254. keys_dir = get_keys_dir()
  255. for name in os.listdir(keys_dir):
  256. filename = os.path.join(keys_dir, name)
  257. with open(filename, 'r') as fd:
  258. line = fd.readline().strip()
  259. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  260. return filename
  261. raise Exception('Key file for repository with ID %s not found' % id)
  262. def load(self, filename, passphrase):
  263. with open(filename, 'r') as fd:
  264. cdata = a2b_base64(''.join(fd.readlines()[1:]).encode('ascii')) # .encode needed for Python 3.[0-2]
  265. data = self.decrypt_key_file(cdata, passphrase)
  266. if data:
  267. key = msgpack.unpackb(data)
  268. if key[b'version'] != 1:
  269. raise IntegrityError('Invalid key file header')
  270. self.repository_id = key[b'repository_id']
  271. self.enc_key = key[b'enc_key']
  272. self.enc_hmac_key = key[b'enc_hmac_key']
  273. self.id_key = key[b'id_key']
  274. self.chunk_seed = key[b'chunk_seed']
  275. self.path = filename
  276. return True
  277. def decrypt_key_file(self, data, passphrase):
  278. d = msgpack.unpackb(data)
  279. assert d[b'version'] == 1
  280. assert d[b'algorithm'] == b'sha256'
  281. key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
  282. data = AES(key).decrypt(d[b'data'])
  283. if HMAC(key, data, sha256).digest() != d[b'hash']:
  284. return None
  285. return data
  286. def encrypt_key_file(self, data, passphrase):
  287. salt = get_random_bytes(32)
  288. iterations = 100000
  289. key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
  290. hash = HMAC(key, data, sha256).digest()
  291. cdata = AES(key).encrypt(data)
  292. d = {
  293. 'version': 1,
  294. 'salt': salt,
  295. 'iterations': iterations,
  296. 'algorithm': 'sha256',
  297. 'hash': hash,
  298. 'data': cdata,
  299. }
  300. return msgpack.packb(d)
  301. def save(self, path, passphrase):
  302. key = {
  303. 'version': 1,
  304. 'repository_id': self.repository_id,
  305. 'enc_key': self.enc_key,
  306. 'enc_hmac_key': self.enc_hmac_key,
  307. 'id_key': self.id_key,
  308. 'chunk_seed': self.chunk_seed,
  309. }
  310. data = self.encrypt_key_file(msgpack.packb(key), passphrase)
  311. with open(path, 'w') as fd:
  312. fd.write('%s %s\n' % (self.FILE_ID, hexlify(self.repository_id).decode('ascii')))
  313. fd.write('\n'.join(textwrap.wrap(b2a_base64(data).decode('ascii'))))
  314. fd.write('\n')
  315. self.path = path
  316. def change_passphrase(self):
  317. passphrase, passphrase2 = 1, 2
  318. while passphrase != passphrase2:
  319. passphrase = getpass('New passphrase: ')
  320. passphrase2 = getpass('Enter same passphrase again: ')
  321. if passphrase != passphrase2:
  322. print('Passphrases do not match')
  323. self.save(self.path, passphrase)
  324. print('Key file "%s" updated' % self.path)
  325. @classmethod
  326. def create(cls, repository, args):
  327. filename = args.repository.to_key_filename()
  328. path = filename
  329. i = 1
  330. while os.path.exists(path):
  331. i += 1
  332. path = filename + '.%d' % i
  333. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  334. if passphrase is not None:
  335. passphrase2 = passphrase
  336. else:
  337. passphrase, passphrase2 = 1, 2
  338. while passphrase != passphrase2:
  339. passphrase = getpass('Enter passphrase (empty for no passphrase):')
  340. passphrase2 = getpass('Enter same passphrase again: ')
  341. if passphrase != passphrase2:
  342. print('Passphrases do not match')
  343. compressor = compressor_creator(args)
  344. maccer = maccer_creator(args, cls)
  345. key = cls(compressor, maccer)
  346. key.repository_id = repository.id
  347. key.init_from_random_data(get_random_bytes(100))
  348. key.init_ciphers()
  349. key.save(path, passphrase)
  350. print('Key file "%s" created.' % key.path)
  351. print('Keep this file safe. Your data will be inaccessible without it.')
  352. return key
  353. # note: key 0 nicely maps to a zlib compressor with level 0 which means "no compression"
  354. compressor_mapping = {}
  355. for level in ZlibCompressor.LEVELS:
  356. compressor_mapping[ZlibCompressor.TYPE + level] = \
  357. type('ZlibCompressorLevel%d' % level, (ZlibCompressor, ), dict(TYPE=ZlibCompressor.TYPE + level))
  358. for preset in LzmaCompressor.PRESETS:
  359. compressor_mapping[LzmaCompressor.TYPE + preset] = \
  360. type('LzmaCompressorPreset%d' % preset, (LzmaCompressor, ), dict(TYPE=LzmaCompressor.TYPE + preset))
  361. crypter_mapping = {
  362. KeyfileKey.TYPE: KeyfileKey,
  363. PassphraseKey.TYPE: PassphraseKey,
  364. PlaintextKey.TYPE: PlaintextKey,
  365. }
  366. maccer_mapping = {
  367. # simple hashes, not MACs (but MAC-like class __init__ method signature):
  368. SHA256.TYPE: SHA256,
  369. SHA512_256.TYPE: SHA512_256,
  370. # MACs:
  371. HMAC_SHA256.TYPE: HMAC_SHA256,
  372. HMAC_SHA512_256.TYPE: HMAC_SHA512_256,
  373. }
  374. def get_implementations(meta):
  375. try:
  376. compressor = compressor_mapping[meta.compr_type]
  377. crypter = crypter_mapping[meta.crypt_type]
  378. maccer = maccer_mapping[meta.mac_type]
  379. except KeyError:
  380. raise UnsupportedPayloadError("compr_type %x crypt_type %x mac_type %x" % (
  381. meta.compr_type, meta.crypt_type, meta.mac_type))
  382. return compressor, crypter, maccer
  383. def legacy_parser(all_data, crypt_type): # all rather hardcoded
  384. """
  385. Payload layout:
  386. no encryption: TYPE(1) + data
  387. with encryption: TYPE(1) + HMAC(32) + NONCE(8) + data
  388. data is compressed with zlib level 6 and (in the 2nd case) encrypted.
  389. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  390. in the payload, the first 8 bytes are always zeros. This does not
  391. affect security but limits the maximum repository capacity to
  392. only 295 exabytes!
  393. """
  394. offset = 1
  395. if crypt_type == PlaintextKey.TYPE:
  396. hmac = None
  397. iv = stored_iv = None
  398. data = all_data[offset:]
  399. else:
  400. hmac = all_data[offset:offset+32]
  401. stored_iv = all_data[offset+32:offset+40]
  402. data = all_data[offset+40:]
  403. meta = Meta(compr_type=6, crypt_type=crypt_type, mac_type=HMAC_SHA256.TYPE,
  404. hmac=hmac, stored_iv=stored_iv)
  405. compressor, crypter, maccer = get_implementations(meta)
  406. return meta, data, compressor, crypter, maccer
  407. def parser00(all_data):
  408. return legacy_parser(all_data, KeyfileKey.TYPE)
  409. def parser01(all_data):
  410. return legacy_parser(all_data, PassphraseKey.TYPE)
  411. def parser02(all_data):
  412. return legacy_parser(all_data, PlaintextKey.TYPE)
  413. def parser03(all_data): # new & flexible
  414. """
  415. Payload layout:
  416. always: TYPE(1) + MSGPACK((meta, data))
  417. meta is a Meta namedtuple and contains all required information about data.
  418. data is maybe compressed (see meta) and maybe encrypted (see meta).
  419. """
  420. # TODO use Unpacker(..., max_*_len=NOTMORETHANNEEDED) to avoid any memory
  421. # allocation issues on untrusted and potentially tampered input data.
  422. # Problem: we currently must use older msgpack because pure python impl.
  423. # is broken in 0.4.2 < version <= 0.4.5, but this api is only offered by
  424. # more recent ones, not by 0.4.2. So, fix here when 0.4.6 is out. :-(
  425. meta_tuple, data = msgpack.unpackb(all_data[1:])
  426. meta = Meta(*meta_tuple)
  427. compressor, crypter, maccer = get_implementations(meta)
  428. return meta, data, compressor, crypter, maccer
  429. def parser(data):
  430. parser_mapping = {
  431. 0x00: parser00,
  432. 0x01: parser01,
  433. 0x02: parser02,
  434. 0x03: parser03,
  435. }
  436. header_type = data[0]
  437. parser_func = parser_mapping[header_type]
  438. return parser_func(data)
  439. def key_factory(repository, manifest_data):
  440. meta, data, compressor, crypter, maccer = parser(manifest_data)
  441. return crypter.detect(repository, manifest_data)
  442. def generate(meta, data):
  443. # always create new-style 0x03 format
  444. return b'\x03' + msgpack.packb((meta, data))
  445. def compressor_creator(args):
  446. # args == None is used by unit tests
  447. compression = COMPR_DEFAULT if args is None else args.compression
  448. compressor = compressor_mapping.get(compression)
  449. if compressor is None:
  450. raise NotImplementedError("no compression %d" % args.compression)
  451. return compressor
  452. def key_creator(repository, args):
  453. if args.encryption == 'keyfile':
  454. return KeyfileKey.create(repository, args)
  455. if args.encryption == 'passphrase':
  456. return PassphraseKey.create(repository, args)
  457. if args.encryption == 'none':
  458. return PlaintextKey.create(repository, args)
  459. raise NotImplemented("no encryption %s" % args.encryption)
  460. def maccer_creator(args, key_cls):
  461. # args == None is used by unit tests
  462. mac = None if args is None else args.mac
  463. if mac is None:
  464. if key_cls is PlaintextKey:
  465. mac = HASH_DEFAULT
  466. elif key_cls in (KeyfileKey, PassphraseKey):
  467. mac = MAC_DEFAULT
  468. else:
  469. raise NotImplementedError("unknown key class")
  470. maccer = maccer_mapping.get(mac)
  471. if maccer is None:
  472. raise NotImplementedError("no mac %d" % args.mac)
  473. return maccer