key.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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(is_encrypt=True, key=self.enc_key, iv=enc_iv)
  190. self.dec_cipher = AES(is_encrypt=False, key=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 change_passphrase(self):
  234. class ImmutablePassphraseError(Error):
  235. """The passphrase for this encryption key type can't be changed."""
  236. raise ImmutablePassphraseError
  237. def init(self, repository, passphrase):
  238. self.init_from_random_data(pbkdf2_sha256(passphrase.encode('utf-8'), repository.id, self.iterations, 100))
  239. self.init_ciphers()
  240. class KeyfileKey(AESKeyBase):
  241. FILE_ID = 'ATTIC KEY'
  242. TYPE = 0x00
  243. @classmethod
  244. def detect(cls, repository, manifest_data):
  245. meta, data, compressor, crypter, maccer = parser(manifest_data)
  246. key = cls(compressor, maccer)
  247. path = cls.find_key_file(repository)
  248. prompt = 'Enter passphrase for key file %s: ' % path
  249. passphrase = os.environ.get('ATTIC_PASSPHRASE', '')
  250. while not key.load(path, passphrase):
  251. passphrase = getpass(prompt)
  252. num_blocks = num_aes_blocks(len(data))
  253. key.init_ciphers(PREFIX + long_to_bytes(key.extract_nonce(manifest_data) + num_blocks))
  254. return key
  255. @classmethod
  256. def find_key_file(cls, repository):
  257. id = hexlify(repository.id).decode('ascii')
  258. keys_dir = get_keys_dir()
  259. for name in os.listdir(keys_dir):
  260. filename = os.path.join(keys_dir, name)
  261. with open(filename, 'r') as fd:
  262. line = fd.readline().strip()
  263. if line and line.startswith(cls.FILE_ID) and line[10:] == id:
  264. return filename
  265. raise Exception('Key file for repository with ID %s not found' % id)
  266. def load(self, filename, passphrase):
  267. with open(filename, 'r') as fd:
  268. cdata = a2b_base64(''.join(fd.readlines()[1:]).encode('ascii')) # .encode needed for Python 3.[0-2]
  269. data = self.decrypt_key_file(cdata, passphrase)
  270. if data:
  271. key = msgpack.unpackb(data)
  272. if key[b'version'] != 1:
  273. raise IntegrityError('Invalid key file header')
  274. self.repository_id = key[b'repository_id']
  275. self.enc_key = key[b'enc_key']
  276. self.enc_hmac_key = key[b'enc_hmac_key']
  277. self.id_key = key[b'id_key']
  278. self.chunk_seed = key[b'chunk_seed']
  279. self.path = filename
  280. return True
  281. def decrypt_key_file(self, data, passphrase):
  282. d = msgpack.unpackb(data)
  283. assert d[b'version'] == 1
  284. assert d[b'algorithm'] == b'sha256'
  285. key = pbkdf2_sha256(passphrase.encode('utf-8'), d[b'salt'], d[b'iterations'], 32)
  286. data = AES(is_encrypt=False, key=key).decrypt(d[b'data'])
  287. if HMAC(key, data, sha256).digest() != d[b'hash']:
  288. return None
  289. return data
  290. def encrypt_key_file(self, data, passphrase):
  291. salt = get_random_bytes(32)
  292. iterations = 100000
  293. key = pbkdf2_sha256(passphrase.encode('utf-8'), salt, iterations, 32)
  294. hash = HMAC(key, data, sha256).digest()
  295. cdata = AES(is_encrypt=True, key=key).encrypt(data)
  296. d = {
  297. 'version': 1,
  298. 'salt': salt,
  299. 'iterations': iterations,
  300. 'algorithm': 'sha256',
  301. 'hash': hash,
  302. 'data': cdata,
  303. }
  304. return msgpack.packb(d)
  305. def save(self, path, passphrase):
  306. key = {
  307. 'version': 1,
  308. 'repository_id': self.repository_id,
  309. 'enc_key': self.enc_key,
  310. 'enc_hmac_key': self.enc_hmac_key,
  311. 'id_key': self.id_key,
  312. 'chunk_seed': self.chunk_seed,
  313. }
  314. data = self.encrypt_key_file(msgpack.packb(key), passphrase)
  315. with open(path, 'w') as fd:
  316. fd.write('%s %s\n' % (self.FILE_ID, hexlify(self.repository_id).decode('ascii')))
  317. fd.write('\n'.join(textwrap.wrap(b2a_base64(data).decode('ascii'))))
  318. fd.write('\n')
  319. self.path = path
  320. def change_passphrase(self):
  321. passphrase, passphrase2 = 1, 2
  322. while passphrase != passphrase2:
  323. passphrase = getpass('New passphrase: ')
  324. passphrase2 = getpass('Enter same passphrase again: ')
  325. if passphrase != passphrase2:
  326. print('Passphrases do not match')
  327. self.save(self.path, passphrase)
  328. print('Key file "%s" updated' % self.path)
  329. @classmethod
  330. def create(cls, repository, args):
  331. filename = args.repository.to_key_filename()
  332. path = filename
  333. i = 1
  334. while os.path.exists(path):
  335. i += 1
  336. path = filename + '.%d' % i
  337. passphrase = os.environ.get('ATTIC_PASSPHRASE')
  338. if passphrase is not None:
  339. passphrase2 = passphrase
  340. else:
  341. passphrase, passphrase2 = 1, 2
  342. while passphrase != passphrase2:
  343. passphrase = getpass('Enter passphrase (empty for no passphrase):')
  344. passphrase2 = getpass('Enter same passphrase again: ')
  345. if passphrase != passphrase2:
  346. print('Passphrases do not match')
  347. compressor = compressor_creator(args)
  348. maccer = maccer_creator(args, cls)
  349. key = cls(compressor, maccer)
  350. key.repository_id = repository.id
  351. key.init_from_random_data(get_random_bytes(100))
  352. key.init_ciphers()
  353. key.save(path, passphrase)
  354. print('Key file "%s" created.' % key.path)
  355. print('Keep this file safe. Your data will be inaccessible without it.')
  356. return key
  357. # note: key 0 nicely maps to a zlib compressor with level 0 which means "no compression"
  358. compressor_mapping = {}
  359. for level in ZlibCompressor.LEVELS:
  360. compressor_mapping[ZlibCompressor.TYPE + level] = \
  361. type('ZlibCompressorLevel%d' % level, (ZlibCompressor, ), dict(TYPE=ZlibCompressor.TYPE + level))
  362. for preset in LzmaCompressor.PRESETS:
  363. compressor_mapping[LzmaCompressor.TYPE + preset] = \
  364. type('LzmaCompressorPreset%d' % preset, (LzmaCompressor, ), dict(TYPE=LzmaCompressor.TYPE + preset))
  365. crypter_mapping = {
  366. KeyfileKey.TYPE: KeyfileKey,
  367. PassphraseKey.TYPE: PassphraseKey,
  368. PlaintextKey.TYPE: PlaintextKey,
  369. }
  370. maccer_mapping = {
  371. # simple hashes, not MACs (but MAC-like class __init__ method signature):
  372. SHA256.TYPE: SHA256,
  373. SHA512_256.TYPE: SHA512_256,
  374. # MACs:
  375. HMAC_SHA256.TYPE: HMAC_SHA256,
  376. HMAC_SHA512_256.TYPE: HMAC_SHA512_256,
  377. }
  378. def get_implementations(meta):
  379. try:
  380. compressor = compressor_mapping[meta.compr_type]
  381. crypter = crypter_mapping[meta.crypt_type]
  382. maccer = maccer_mapping[meta.mac_type]
  383. except KeyError:
  384. raise UnsupportedPayloadError("compr_type %x crypt_type %x mac_type %x" % (
  385. meta.compr_type, meta.crypt_type, meta.mac_type))
  386. return compressor, crypter, maccer
  387. def legacy_parser(all_data, crypt_type): # all rather hardcoded
  388. """
  389. Payload layout:
  390. no encryption: TYPE(1) + data
  391. with encryption: TYPE(1) + HMAC(32) + NONCE(8) + data
  392. data is compressed with zlib level 6 and (in the 2nd case) encrypted.
  393. To reduce payload size only 8 bytes of the 16 bytes nonce is saved
  394. in the payload, the first 8 bytes are always zeros. This does not
  395. affect security but limits the maximum repository capacity to
  396. only 295 exabytes!
  397. """
  398. offset = 1
  399. if crypt_type == PlaintextKey.TYPE:
  400. hmac = None
  401. iv = stored_iv = None
  402. data = all_data[offset:]
  403. else:
  404. hmac = all_data[offset:offset+32]
  405. stored_iv = all_data[offset+32:offset+40]
  406. data = all_data[offset+40:]
  407. meta = Meta(compr_type=6, crypt_type=crypt_type, mac_type=HMAC_SHA256.TYPE,
  408. hmac=hmac, stored_iv=stored_iv)
  409. compressor, crypter, maccer = get_implementations(meta)
  410. return meta, data, compressor, crypter, maccer
  411. def parser00(all_data):
  412. return legacy_parser(all_data, KeyfileKey.TYPE)
  413. def parser01(all_data):
  414. return legacy_parser(all_data, PassphraseKey.TYPE)
  415. def parser02(all_data):
  416. return legacy_parser(all_data, PlaintextKey.TYPE)
  417. def parser03(all_data): # new & flexible
  418. """
  419. Payload layout:
  420. always: TYPE(1) + MSGPACK((meta, data))
  421. meta is a Meta namedtuple and contains all required information about data.
  422. data is maybe compressed (see meta) and maybe encrypted (see meta).
  423. """
  424. # TODO use Unpacker(..., max_*_len=NOTMORETHANNEEDED) to avoid any memory
  425. # allocation issues on untrusted and potentially tampered input data.
  426. # Problem: we currently must use older msgpack because pure python impl.
  427. # is broken in 0.4.2 < version <= 0.4.5, but this api is only offered by
  428. # more recent ones, not by 0.4.2. So, fix here when 0.4.6 is out. :-(
  429. meta_tuple, data = msgpack.unpackb(all_data[1:])
  430. meta = Meta(*meta_tuple)
  431. compressor, crypter, maccer = get_implementations(meta)
  432. return meta, data, compressor, crypter, maccer
  433. def parser(data):
  434. parser_mapping = {
  435. 0x00: parser00,
  436. 0x01: parser01,
  437. 0x02: parser02,
  438. 0x03: parser03,
  439. }
  440. header_type = data[0]
  441. parser_func = parser_mapping[header_type]
  442. return parser_func(data)
  443. def key_factory(repository, manifest_data):
  444. meta, data, compressor, crypter, maccer = parser(manifest_data)
  445. return crypter.detect(repository, manifest_data)
  446. def generate(meta, data):
  447. # always create new-style 0x03 format
  448. return b'\x03' + msgpack.packb((meta, data))
  449. def compressor_creator(args):
  450. # args == None is used by unit tests
  451. compression = COMPR_DEFAULT if args is None else args.compression
  452. compressor = compressor_mapping.get(compression)
  453. if compressor is None:
  454. raise NotImplementedError("no compression %d" % args.compression)
  455. return compressor
  456. def key_creator(repository, args):
  457. if args.encryption == 'keyfile':
  458. return KeyfileKey.create(repository, args)
  459. if args.encryption == 'passphrase':
  460. return PassphraseKey.create(repository, args)
  461. if args.encryption == 'none':
  462. return PlaintextKey.create(repository, args)
  463. raise NotImplemented("no encryption %s" % args.encryption)
  464. def maccer_creator(args, key_cls):
  465. # args == None is used by unit tests
  466. mac = None if args is None else args.mac
  467. if mac is None:
  468. if key_cls is PlaintextKey:
  469. mac = HASH_DEFAULT
  470. elif key_cls in (KeyfileKey, PassphraseKey):
  471. mac = MAC_DEFAULT
  472. else:
  473. raise NotImplementedError("unknown key class")
  474. maccer = maccer_mapping.get(mac)
  475. if maccer is None:
  476. raise NotImplementedError("no mac %d" % args.mac)
  477. return maccer