Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow keyfile to be passed as bytes #364

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 45 additions & 36 deletions pykeepass/kdbx_parsing/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ def aes_kdf(key, rounds, key_composite):
return hashlib.sha256(transformed_key).digest()


def load_keyfile_composite(key: bytes):
try:
int(key, 16)
is_hex = True
except ValueError:
is_hex = False
# if the length is 32 bytes we assume it is the key
if len(key) == 32:
keyfile_composite = key
# if the length is 64 bytes we assume the key is hex encoded
elif len(key) == 64 and is_hex:
keyfile_composite = codecs.decode(key, 'hex')
# anything else may be a file to hash for the key
else:
keyfile_composite = hashlib.sha256(key).digest()

return keyfile_composite


def compute_key_composite(password=None, keyfile=None):
"""Compute composite key.
Used in header verification and payload decryption."""
Expand All @@ -116,43 +135,33 @@ def compute_key_composite(password=None, keyfile=None):
password_composite = b''
# hash the keyfile
if keyfile:
# try to read XML keyfile
try:
with open(keyfile, 'r') as f:
tree = etree.parse(f).getroot()
version = tree.find('Meta/Version').text
data_element = tree.find('Key/Data')
if version.startswith('1.0'):
keyfile_composite = base64.b64decode(data_element.text)
elif version.startswith('2.0'):
# read keyfile data and convert to bytes
keyfile_composite = bytes.fromhex(data_element.text.strip())
# validate bytes against hash
hash = bytes.fromhex(data_element.attrib['Hash'])
hash_computed = hashlib.sha256(keyfile_composite).digest()[:4]
assert hash == hash_computed, "Keyfile has invalid hash"
# otherwise, try to read plain keyfile
except (etree.XMLSyntaxError, UnicodeDecodeError):
if isinstance(keyfile, bytes):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the save method, where it checks whether the file has hasattr(filename, "write") maybe the same could be done with the read attribute. BytesIO has this btw.

Copy link
Contributor Author

@janbrummer janbrummer Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case i'd need to change it from bytes to BytesIO. Not sure we want to enforce this in apps using pykeepass? No strong opinion here, up to the reviewers i guess.

keyfile_composite = keyfile_composite = load_keyfile_composite(keyfile)
else:
# try to read XML keyfile
try:
with open(keyfile, 'rb') as f:
key = f.read()

try:
int(key, 16)
is_hex = True
except ValueError:
is_hex = False
# if the length is 32 bytes we assume it is the key
if len(key) == 32:
keyfile_composite = key
# if the length is 64 bytes we assume the key is hex encoded
elif len(key) == 64 and is_hex:
keyfile_composite = codecs.decode(key, 'hex')
# anything else may be a file to hash for the key
else:
keyfile_composite = hashlib.sha256(key).digest()
except:
raise IOError('Could not read keyfile')
with open(keyfile, 'r') as f:
tree = etree.parse(f).getroot()
version = tree.find('Meta/Version').text
data_element = tree.find('Key/Data')
if version.startswith('1.0'):
keyfile_composite = base64.b64decode(data_element.text)
elif version.startswith('2.0'):
# read keyfile data and convert to bytes
keyfile_composite = bytes.fromhex(data_element.text.strip())
# validate bytes against hash
hash = bytes.fromhex(data_element.attrib['Hash'])
hash_computed = hashlib.sha256(keyfile_composite).digest()[:4]
assert hash == hash_computed, "Keyfile has invalid hash"
# otherwise, try to read plain keyfile
except (etree.XMLSyntaxError, UnicodeDecodeError):
try:
with open(keyfile, 'rb') as f:
key = f.read()

keyfile_composite = load_keyfile_composite(key)
except:
raise IOError('Could not read keyfile')

else:
keyfile_composite = b''
Expand Down
23 changes: 23 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,29 @@ def test_open_no_decrypt(self):

self.assertEqual(kp.database_salt, salt)


def test_keyfile_as_bytes(self):

databases = [
'test4.kdbx',
]
passwords = [
'password',
]
keyfiles = [
base_dir + '/test4.key'
]
for database, password, keyfile in zip(databases, passwords, keyfiles):
with open(keyfile, "rb") as fh:
buf = fh.read()

kp = PyKeePass(
os.path.join(base_dir, database),
password,
keyfile=buf
)


if __name__ == '__main__':
unittest.main()

Loading