Skip to content

Commit 080f8f7

Browse files
committed
tpm: T7726: Test TPM key or prompt recovery key
1 parent 6329c47 commit 080f8f7

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/helpers/vyos-config-encrypt.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
mount_path_old = f'{mount_path}.old'
3535
dm_device = '/dev/mapper/vyos_config'
3636

37+
3738
def is_opened():
3839
return os.path.exists(dm_device)
3940

@@ -146,6 +147,29 @@ def config_backup_folder(base):
146147
idx += 1
147148
return f'{base}.{idx}'
148149

150+
def test_decrypt(key):
151+
if not key:
152+
return
153+
154+
persist_path = cmd(persistpath_cmd).strip()
155+
image_name = get_running_image()
156+
image_path = os.path.join(persist_path, 'luks', image_name)
157+
158+
key_file = None
159+
160+
if not is_opened():
161+
with NamedTemporaryFile(dir='/dev/shm', delete=False) as f:
162+
f.write(key)
163+
key_file = f.name
164+
165+
try:
166+
cmd(f'cryptsetup -q open {image_path} vyos_config --key-file={key_file}')
167+
os.unlink(key_file)
168+
return True
169+
except:
170+
return False
171+
return False
172+
149173
def decrypt_config(key):
150174
if not key:
151175
return
@@ -258,10 +282,10 @@ def decrypt_config(key):
258282
else:
259283
key = Fernet.generate_key()
260284
elif args.disable or args.load:
261-
if existing_key:
285+
if existing_key and test_decrypt(existing_key):
262286
need_recovery = False
263287
else:
264-
print('Failed to read key from TPM, recovery key required')
288+
print('TPM key invalid or not found, recovery key required')
265289
need_recovery = True
266290
else:
267291
need_recovery = True

0 commit comments

Comments
 (0)