Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/archinstall/lib/disk/encryption_menu.py
blob: b0e292cec72cf41f5c9b05be142c292822eed385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from pathlib import Path
from typing import Dict, Optional, Any, TYPE_CHECKING, List

from . import LvmConfiguration, LvmVolume
from ..disk import (
	DeviceModification,
	DiskLayoutConfiguration,
	PartitionModification,
	DiskEncryption,
	EncryptionType
)
from ..menu import (
	Selector,
	AbstractSubMenu,
	MenuSelectionType,
	TableMenu
)
from ..interactions.utils import get_password
from ..menu import Menu
from ..general import secret
from .fido import Fido2Device, Fido2
from ..output import FormattedOutput

if TYPE_CHECKING:
	_: Any


class DiskEncryptionMenu(AbstractSubMenu):
	def __init__(
		self,
		disk_config: DiskLayoutConfiguration,
		data_store: Dict[str, Any],
		preset: Optional[DiskEncryption] = None
	):
		if preset:
			self._preset = preset
		else:
			self._preset = DiskEncryption()

		self._disk_config = disk_config
		super().__init__(data_store=data_store)

	def setup_selection_menu_options(self):
		self._menu_options['encryption_type'] = \
			Selector(
				_('Encryption type'),
				func=lambda preset: select_encryption_type(self._disk_config, preset),
				display_func=lambda x: EncryptionType.type_to_text(x) if x else None,
				default=self._preset.encryption_type,
				enabled=True,
			)
		self._menu_options['encryption_password'] = \
			Selector(
				_('Encryption password'),
				lambda x: select_encrypted_password(),
				dependencies=[self._check_dep_enc_type],
				display_func=lambda x: secret(x) if x else '',
				default=self._preset.encryption_password,
				enabled=True
			)
		self._menu_options['partitions'] = \
			Selector(
				_('Partitions'),
				func=lambda preset: select_partitions_to_encrypt(self._disk_config.device_modifications, preset),
				display_func=lambda x: f'{len(x)} {_("Partitions")}' if x else None,
				dependencies=[self._check_dep_partitions],
				default=self._preset.partitions,
				preview_func=self._prev_partitions,
				enabled=True
			)
		self._menu_options['lvm_vols'] = \
			Selector(
				_('LVM volumes'),
				func=lambda preset: self._select_lvm_vols(preset),
				display_func=lambda x: f'{len(x)} {_("LVM volumes")}' if x else None,
				dependencies=[self._check_dep_lvm_vols],
				default=self._preset.lvm_volumes,
				preview_func=self._prev_lvm_vols,
				enabled=True
			)
		self._menu_options['HSM'] = \
			Selector(
				description=_('Use HSM to unlock encrypted drive'),
				func=lambda preset: select_hsm(preset),
				display_func=lambda x: self._display_hsm(x),
				preview_func=self._prev_hsm,
				dependencies=[self._check_dep_enc_type],
				default=self._preset.hsm_device,
				enabled=True
			)

	def _select_lvm_vols(self, preset: List[LvmVolume]) -> List[LvmVolume]:
		if self._disk_config.lvm_config:
			return select_lvm_vols_to_encrypt(self._disk_config.lvm_config, preset=preset)
		return []

	def _check_dep_enc_type(self) -> bool:
		enc_type: Optional[EncryptionType] = self._menu_options['encryption_type'].current_selection
		if enc_type and enc_type != EncryptionType.NoEncryption:
			return True
		return False

	def _check_dep_partitions(self) -> bool:
		enc_type: Optional[EncryptionType] = self._menu_options['encryption_type'].current_selection
		if enc_type and enc_type in [EncryptionType.Luks, EncryptionType.LvmOnLuks]:
			return True
		return False

	def _check_dep_lvm_vols(self) -> bool:
		enc_type: Optional[EncryptionType] = self._menu_options['encryption_type'].current_selection
		if enc_type and enc_type == EncryptionType.LuksOnLvm:
			return True
		return False

	def run(self, allow_reset: bool = True) -> Optional[DiskEncryption]:
		super().run(allow_reset=allow_reset)

		enc_type = self._data_store.get('encryption_type', None)
		enc_password = self._data_store.get('encryption_password', None)
		enc_partitions = self._data_store.get('partitions', None)
		enc_lvm_vols = self._data_store.get('lvm_vols', None)

		if enc_type in [EncryptionType.Luks, EncryptionType.LvmOnLuks] and enc_partitions:
			enc_lvm_vols = []

		if enc_type == EncryptionType.LuksOnLvm:
			enc_partitions = []

		if enc_type != EncryptionType.NoEncryption and enc_password and (enc_partitions or enc_lvm_vols):
			return DiskEncryption(
				encryption_password=enc_password,
				encryption_type=enc_type,
				partitions=enc_partitions,
				lvm_volumes=enc_lvm_vols,
				hsm_device=self._data_store.get('HSM', None)
			)

		return None

	def _display_hsm(self, device: Optional[Fido2Device]) -> Optional[str]:
		if device:
			return device.manufacturer

		return None

	def _prev_partitions(self) -> Optional[str]:
		partitions: Optional[List[PartitionModification]] = self._menu_options['partitions'].current_selection
		if partitions:
			output = str(_('Partitions to be encrypted')) + '\n'
			output += FormattedOutput.as_table(partitions)
			return output.rstrip()

		return None

	def _prev_lvm_vols(self) -> Optional[str]:
		volumes: Optional[List[PartitionModification]] = self._menu_options['lvm_vols'].current_selection
		if volumes:
			output = str(_('LVM volumes to be encrypted')) + '\n'
			output += FormattedOutput.as_table(volumes)
			return output.rstrip()

		return None

	def _prev_hsm(self) -> Optional[str]:
		try:
			Fido2.get_fido2_devices()
		except ValueError:
			return str(_('Unable to determine fido2 devices. Is libfido2 installed?'))

		fido_device: Optional[Fido2Device] = self._menu_options['HSM'].current_selection

		if fido_device:
			output = '{}: {}'.format(str(_('Path')), fido_device.path)
			output += '{}: {}'.format(str(_('Manufacturer')), fido_device.manufacturer)
			output += '{}: {}'.format(str(_('Product')), fido_device.product)
			return output

		return None


def select_encryption_type(disk_config: DiskLayoutConfiguration, preset: EncryptionType) -> Optional[EncryptionType]:
	title = str(_('Select disk encryption option'))

	if disk_config.lvm_config:
		options = [
			EncryptionType.type_to_text(EncryptionType.LvmOnLuks),
			EncryptionType.type_to_text(EncryptionType.LuksOnLvm)
		]
	else:
		options = [EncryptionType.type_to_text(EncryptionType.Luks)]

	preset_value = EncryptionType.type_to_text(preset)

	choice = Menu(title, options, preset_values=preset_value).run()

	match choice.type_:
		case MenuSelectionType.Reset: return None
		case MenuSelectionType.Skip: return preset
		case MenuSelectionType.Selection: return EncryptionType.text_to_type(choice.value)  # type: ignore


def select_encrypted_password() -> Optional[str]:
	if passwd := get_password(prompt=str(_('Enter disk encryption password (leave blank for no encryption): '))):
		return passwd
	return None


def select_hsm(preset: Optional[Fido2Device] = None) -> Optional[Fido2Device]:
	title = _('Select a FIDO2 device to use for HSM')

	try:
		fido_devices = Fido2.get_fido2_devices()
	except ValueError:
		return None

	if fido_devices:
		choice = TableMenu(title, data=fido_devices).run()
		match choice.type_:
			case MenuSelectionType.Reset:
				return None
			case MenuSelectionType.Skip:
				return preset
			case MenuSelectionType.Selection:
				return choice.value  # type: ignore

	return None


def select_partitions_to_encrypt(
	modification: List[DeviceModification],
	preset: List[PartitionModification]
) -> List[PartitionModification]:
	partitions: List[PartitionModification] = []

	# do not allow encrypting the boot partition
	for mod in modification:
		partitions += list(filter(lambda x: x.mountpoint != Path('/boot'), mod.partitions))

	# do not allow encrypting existing partitions that are not marked as wipe
	avail_partitions = list(filter(lambda x: not x.exists(), partitions))

	if avail_partitions:
		title = str(_('Select which partitions to encrypt'))
		partition_table = FormattedOutput.as_table(avail_partitions)

		choice = TableMenu(
			title,
			table_data=(avail_partitions, partition_table),
			preset=preset,
			multi=True
		).run()

		match choice.type_:
			case MenuSelectionType.Reset:
				return []
			case MenuSelectionType.Skip:
				return preset
			case MenuSelectionType.Selection:
				return choice.multi_value
	return []


def select_lvm_vols_to_encrypt(
	lvm_config: LvmConfiguration,
	preset: List[LvmVolume]
) -> List[LvmVolume]:
	volumes: List[LvmVolume] = lvm_config.get_all_volumes()

	if volumes:
		title = str(_('Select which LVM volumes to encrypt'))
		partition_table = FormattedOutput.as_table(volumes)

		choice = TableMenu(
			title,
			table_data=(volumes, partition_table),
			preset=preset,
			multi=True
		).run()

		match choice.type_:
			case MenuSelectionType.Reset:
				return []
			case MenuSelectionType.Skip:
				return preset
			case MenuSelectionType.Selection:
				return choice.multi_value

	return []