Skip to content

Commit 2bfa84a

Browse files
committed
support 2.0 keyfiles
1 parent 1387ff9 commit 2bfa84a

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

pykeepass/kdbx_parsing/common.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ def compute_key_composite(password=None, keyfile=None):
119119
try:
120120
with open(keyfile, 'r') as f:
121121
tree = etree.parse(f).getroot()
122-
keyfile_composite = base64.b64decode(tree.find('Key/Data').text)
122+
version = tree.find('Meta/Version').text
123+
data_element = tree.find('Key/Data')
124+
if version.startswith('1.0'):
125+
keyfile_composite = base64.b64decode(data_element.text)
126+
elif version.startswith('2.0'):
127+
# read keyfile data and convert to bytes
128+
keyfile_composite = bytes.fromhex(data_element.text.strip())
129+
# validate bytes against hash
130+
hash = bytes.fromhex(data_element.attrib['Hash'])
131+
hash_computed = hashlib.sha256(keyfile_composite).digest()[:4]
132+
assert hash == hash_computed, "Keyfile has invalid hash"
123133
# otherwise, try to read plain keyfile
124134
except (etree.XMLSyntaxError, UnicodeDecodeError):
125135
try:

tests/test4_keyx.kdbx

1.81 KB
Binary file not shown.

tests/test4_keyx.keyx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<KeyFile>
3+
<Meta>
4+
<Version>2.0</Version>
5+
</Meta>
6+
<Key>
7+
<Data Hash="F79BE54D">
8+
30D73184 FBE1C7C4 B07EE4D6 BC4F118B
9+
87577CAB 5CB8846F 5FD286FF F98BF9A9
10+
</Data>
11+
</Key>
12+
</KeyFile>

0 commit comments

Comments
 (0)