Debian Codegraph @

src/plugins/crypto.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Vratislav Podzimek <[email protected]>
18 */
19
20 #include <string.h>
21 #include <glib.h>
22 #include <libcryptsetup.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <linux/random.h>
26 #include <locale.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <blkid.h>
30 #include <sys/types.h>
31 #include <keyutils.h>
32 #include <blockdev/utils.h>
33
34 #ifdef WITH_BD_ESCROW
35 #include <nss.h>
36 #include <volume_key/libvolume_key.h>
37 #endif
38
39 #ifdef HAVE_LINUX_OPAL
40 #include <linux/sed-opal.h>
41 #endif
42
43 #include "crypto.h"
44
45 #ifdef __clang__
46 #define ZERO_INIT {}
47 #else
48 #define ZERO_INIT {0}
49 #endif
50
51 #define SECTOR_SIZE 512
52
53 #define DEFAULT_LUKS_KEYSIZE_BITS 256
54 #define DEFAULT_LUKS_CIPHER "aes-xts-plain64"
55
56 #define DEFAULT_OPAL_KEYSIZE_BITS 256
57
58 #define SQUARE_LOWER_LIMIT 136
59 #define SQUARE_UPPER_LIMIT 426
60 #define SQUARE_BYTES_TO_CHECK 512
61
62 #ifdef LIBCRYPTSETUP_24
63 /* 0 for autodetect since 2.4.0 */
64 #define DEFAULT_LUKS2_SECTOR_SIZE 0
65 #else
66 #define DEFAULT_LUKS2_SECTOR_SIZE 512
67 #endif
68
69
70 /**
71 * SECTION: crypto
72 * @short_description: plugin for operations with encrypted devices
73 * @title: Crypto
74 * @include: crypto.h
75 *
76 * A plugin for operations with encrypted devices. Currently LUKS, Integrity,
77 * TrueCrypt/VeraCrypt, BitLocker and FileVault2 devices are supported.
78 * See %BDCryptoTech for full list of supported technologies.
79 *
80 * Functions taking a parameter called "device" require the backing device to be
81 * passed. On the other hand functions taking the "luks_device" parameter
82 * require the LUKS device (/dev/mapper/SOMETHING").
83 *
84 * Sizes are given in bytes unless stated otherwise.
85 */
86
87 BDCryptoLUKSPBKDF* bd_crypto_luks_pbkdf_copy (BDCryptoLUKSPBKDF *pbkdf) {
88 if (pbkdf == NULL)
89 return NULL;
90
91 BDCryptoLUKSPBKDF *new_pbkdf = g_new0 (BDCryptoLUKSPBKDF, 1);
92 new_pbkdf->type = g_strdup (pbkdf->type);
93 new_pbkdf->hash = g_strdup (pbkdf->hash);
94 new_pbkdf->max_memory_kb = pbkdf->max_memory_kb;
95 new_pbkdf->iterations = pbkdf->iterations;
96 new_pbkdf->time_ms = pbkdf->time_ms;
97 new_pbkdf->parallel_threads = pbkdf->parallel_threads;
98
99 return new_pbkdf;
100 }
101
102 void bd_crypto_luks_pbkdf_free (BDCryptoLUKSPBKDF *pbkdf) {
103 if (pbkdf == NULL)
104 return;
105
106 g_free (pbkdf->type);
107 g_free (pbkdf->hash);
108 g_free (pbkdf);
109 }
110
111 BDCryptoLUKSPBKDF* bd_crypto_luks_pbkdf_new (const gchar *type, const gchar *hash, guint32 max_memory_kb, guint32 iterations, guint32 time_ms, guint32 parallel_threads) {
112 BDCryptoLUKSPBKDF *ret = g_new0 (BDCryptoLUKSPBKDF, 1);
113 ret->type = g_strdup (type);
114 ret->hash = g_strdup (hash);
115 ret->max_memory_kb = max_memory_kb;
116 ret->iterations = iterations;
117 ret->time_ms = time_ms;
118 ret->parallel_threads = parallel_threads;
119
120 return ret;
121 }
122
123 BDCryptoLUKSExtra* bd_crypto_luks_extra_copy (BDCryptoLUKSExtra *extra) {
124 if (extra == NULL)
125 return NULL;
126
127 BDCryptoLUKSExtra *new_extra = g_new0 (BDCryptoLUKSExtra, 1);
128
129 new_extra->integrity = g_strdup (extra->integrity);
130 new_extra->data_alignment = extra->data_alignment;
131 new_extra->data_device = g_strdup (extra->data_device);
132 new_extra->sector_size = extra->sector_size;
133 new_extra->label = g_strdup (extra->label);
134 new_extra->subsystem = g_strdup (extra->subsystem);
135 new_extra->pbkdf = bd_crypto_luks_pbkdf_copy (extra->pbkdf);
136
137 return new_extra;
138 }
139
140 void bd_crypto_luks_extra_free (BDCryptoLUKSExtra *extra) {
141 if (extra == NULL)
142 return;
143
144 g_free (extra->integrity);
145 g_free (extra->data_device);
146 g_free (extra->label);
147 g_free (extra->subsystem);
148 bd_crypto_luks_pbkdf_free (extra->pbkdf);
149 g_free (extra);
150 }
151
152 BDCryptoLUKSExtra* bd_crypto_luks_extra_new (guint64 data_alignment, const gchar *data_device, const gchar *integrity, guint32 sector_size, const gchar *label, const gchar *subsystem, BDCryptoLUKSPBKDF *pbkdf) {
153 BDCryptoLUKSExtra *ret = g_new0 (BDCryptoLUKSExtra, 1);
154 ret->integrity = g_strdup (integrity);
155 ret->data_alignment = data_alignment;
156 ret->data_device = g_strdup (data_device);
157 ret->sector_size = sector_size;
158 ret->label = g_strdup (label);
159 ret->subsystem = g_strdup (subsystem);
160 ret->pbkdf = bd_crypto_luks_pbkdf_copy (pbkdf);
161
162 return ret;
163 }
164
165 BDCryptoIntegrityExtra* bd_crypto_integrity_extra_new (guint32 sector_size, guint64 journal_size, guint journal_watermark, guint journal_commit_time, guint64 interleave_sectors, guint64 tag_size, guint64 buffer_sectors) {
166 BDCryptoIntegrityExtra *ret = g_new0 (BDCryptoIntegrityExtra, 1);
167 ret->sector_size = sector_size;
168 ret->journal_size = journal_size;
169 ret->journal_watermark = journal_watermark;
170 ret->journal_commit_time = journal_commit_time;
171 ret->interleave_sectors = interleave_sectors;
172 ret->tag_size = tag_size;
173 ret->buffer_sectors = buffer_sectors;
174
175 return ret;
176 }
177
178 BDCryptoIntegrityExtra* bd_crypto_integrity_extra_copy (BDCryptoIntegrityExtra *extra) {
179 if (extra == NULL)
180 return NULL;
181
182 BDCryptoIntegrityExtra *new_extra = g_new0 (BDCryptoIntegrityExtra, 1);
183
184 new_extra->sector_size = extra->sector_size;
185 new_extra->journal_size = extra->journal_size;
186 new_extra->journal_watermark = extra->journal_watermark;
187 new_extra->journal_commit_time = extra->journal_commit_time;
188 new_extra->interleave_sectors = extra->interleave_sectors;
189 new_extra->tag_size = extra->tag_size;
190 new_extra->buffer_sectors = extra->buffer_sectors;
191
192 return new_extra;
193 }
194
195 void bd_crypto_integrity_extra_free (BDCryptoIntegrityExtra *extra) {
196 if (extra == NULL)
197 return;
198
199 g_free (extra);
200 }
201
202 void bd_crypto_luks_info_free (BDCryptoLUKSInfo *info) {
203 if (info == NULL)
204 return;
205
206 g_free (info->cipher);
207 g_free (info->mode);
208 g_free (info->uuid);
209 g_free (info->backing_device);
210 g_free (info->label);
211 g_free (info->subsystem);
212 g_free (info);
213 }
214
215 BDCryptoLUKSInfo* bd_crypto_luks_info_copy (BDCryptoLUKSInfo *info) {
216 if (info == NULL)
217 return NULL;
218
219 BDCryptoLUKSInfo *new_info = g_new0 (BDCryptoLUKSInfo, 1);
220
221 new_info->version = info->version;
222 new_info->cipher = g_strdup (info->cipher);
223 new_info->mode = g_strdup (info->mode);
224 new_info->uuid = g_strdup (info->uuid);
225 new_info->backing_device = g_strdup (info->backing_device);
226 new_info->sector_size = info->sector_size;
227 new_info->metadata_size = info->metadata_size;
228 new_info->label = g_strdup (info->label);
229 new_info->subsystem = g_strdup (info->subsystem);
230 new_info->hw_encryption = info->hw_encryption;
231
232 return new_info;
233 }
234
235 void bd_crypto_bitlk_info_free (BDCryptoBITLKInfo *info) {
236 if (info == NULL)
237 return;
238
239 g_free (info->cipher);
240 g_free (info->mode);
241 g_free (info->uuid);
242 g_free (info->backing_device);
243 g_free (info);
244 }
245
246 BDCryptoBITLKInfo* bd_crypto_bitlk_info_copy (BDCryptoBITLKInfo *info) {
247 if (info == NULL)
248 return NULL;
249
250 BDCryptoBITLKInfo *new_info = g_new0 (BDCryptoBITLKInfo, 1);
251
252 new_info->cipher = g_strdup (info->cipher);
253 new_info->mode = g_strdup (info->mode);
254 new_info->uuid = g_strdup (info->uuid);
255 new_info->backing_device = g_strdup (info->backing_device);
256 new_info->sector_size = info->sector_size;
257
258 return new_info;
259 }
260
261 void bd_crypto_integrity_info_free (BDCryptoIntegrityInfo *info) {
262 if (info == NULL)
263 return;
264
265 g_free (info->algorithm);
266 g_free (info->journal_crypt);
267 g_free (info->journal_integrity);
268 g_free (info);
269 }
270
271 BDCryptoIntegrityInfo* bd_crypto_integrity_info_copy (BDCryptoIntegrityInfo *info) {
272 if (info == NULL)
273 return NULL;
274
275 BDCryptoIntegrityInfo *new_info = g_new0 (BDCryptoIntegrityInfo, 1);
276
277 new_info->algorithm = g_strdup (info->algorithm);
278 new_info->key_size = info->key_size;
279 new_info->sector_size = info->sector_size;
280 new_info->tag_size = info->tag_size;
281 new_info->interleave_sectors = info->interleave_sectors;
282 new_info->journal_size = info->journal_size;
283 new_info->journal_crypt = g_strdup (info->journal_crypt);
284 new_info->journal_integrity = g_strdup (info->journal_integrity);
285
286 return new_info;
287 }
288
289 void bd_crypto_luks_token_info_free (BDCryptoLUKSTokenInfo *info) {
290 if (info == NULL)
291 return;
292
293 g_free (info->type);
294 g_free (info);
295 }
296
297 BDCryptoLUKSTokenInfo* bd_crypto_luks_token_info_copy (BDCryptoLUKSTokenInfo *info) {
298 if (info == NULL)
299 return NULL;
300
301 BDCryptoLUKSTokenInfo *new_info = g_new0 (BDCryptoLUKSTokenInfo, 1);
302
303 new_info->id = info->id;
304 new_info->type = g_strdup (info->type);
305 new_info->keyslot = info->keyslot;
306
307 return new_info;
308 }
309
310 /* "C" locale to get the locale-agnostic error messages */
311 static locale_t c_locale = (locale_t) 0;
312
313 static void crypto_log_redirect (gint level, const gchar *msg, void *usrptr G_GNUC_UNUSED) {
314 gchar *message = NULL;
315
316 switch (level) {
317 case CRYPT_LOG_DEBUG:
318 case CRYPT_LOG_VERBOSE:
319 message = g_strdup_printf ("[cryptsetup] %s", msg);
320 bd_utils_log (BD_UTILS_LOG_DEBUG, message);
321 g_free (message);
322 break;
323 case CRYPT_LOG_NORMAL:
324 case CRYPT_LOG_ERROR:
325 message = g_strdup_printf ("[cryptsetup] %s", msg);
326 bd_utils_log (BD_UTILS_LOG_INFO, message);
327 g_free (message);
328 break;
329 default:
330 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Unknown cryptsetup log level %d.", level);
331 message = g_strdup_printf ("[cryptsetup] %s", msg);
332 bd_utils_log (BD_UTILS_LOG_INFO, message);
333 g_free (message);
334 break;
335
336 }
337 }
338
339 /**
340 * bd_crypto_init:
341 *
342 * Initializes the plugin. **This function is called automatically by the
343 * library's initialization functions.**
344 *
345 */
346 gboolean bd_crypto_init (void) {
347 #ifdef DEBUG
348 crypt_set_debug_level (CRYPT_DEBUG_ALL);
349 #endif
350 c_locale = newlocale (LC_ALL_MASK, "C", c_locale);
351 crypt_set_log_callback (NULL, &crypto_log_redirect, NULL);
352 return TRUE;
353 }
354
355 /**
356 * bd_crypto_close:
357 *
358 * Cleans up after the plugin. **This function is called automatically by the
359 * library's functions that unload it.**
360 *
361 */
362 void bd_crypto_close (void) {
363 freelocale (c_locale);
364 c_locale = (locale_t) 0;
365 crypt_set_log_callback (NULL, NULL, NULL);
366 crypt_set_debug_level (CRYPT_DEBUG_NONE);
367 }
368
369 /**
370 * bd_crypto_is_tech_avail:
371 * @tech: the queried tech
372 * @mode: a bit mask of queried modes of operation (#BDCryptoTechMode) for @tech
373 * @error: (out) (optional): place to store error (details about why the @tech-@mode combination is not available)
374 *
375 * Returns: whether the @tech-@mode combination is available -- supported by the
376 * plugin implementation and having all the runtime dependencies available
377 */
378 gboolean bd_crypto_is_tech_avail (BDCryptoTech tech, guint64 mode, GError **error) {
379 guint64 ret = 0;
380 switch (tech) {
381 case BD_CRYPTO_TECH_LUKS:
382 ret = mode & (BD_CRYPTO_TECH_MODE_CREATE|BD_CRYPTO_TECH_MODE_OPEN_CLOSE|BD_CRYPTO_TECH_MODE_QUERY|
383 BD_CRYPTO_TECH_MODE_ADD_KEY|BD_CRYPTO_TECH_MODE_REMOVE_KEY|BD_CRYPTO_TECH_MODE_RESIZE|
384 BD_CRYPTO_TECH_MODE_SUSPEND_RESUME|BD_CRYPTO_TECH_MODE_BACKUP_RESTORE|BD_CRYPTO_TECH_MODE_MODIFY);
385 if (ret != mode) {
386 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
387 "Only 'create', 'open', 'query', 'add-key', 'remove-key', 'resize', 'suspend-resume', 'backup-restore', 'modify' supported for LUKS");
388 return FALSE;
389 } else
390 return TRUE;
391 case BD_CRYPTO_TECH_TRUECRYPT:
392 ret = mode & BD_CRYPTO_TECH_MODE_OPEN_CLOSE;
393 if (ret != mode) {
394 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
395 "Only 'open' supported for TrueCrypt");
396 return FALSE;
397 } else
398 return TRUE;
399 case BD_CRYPTO_TECH_ESCROW:
400 #ifndef WITH_BD_ESCROW
401 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
402 "Escrow technology is not available, libblockdev has been compiled without escrow support.");
403 return FALSE;
404 #endif
405 ret = mode & BD_CRYPTO_TECH_MODE_CREATE;
406 if (ret != mode) {
407 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
408 "Only 'create' supported for device escrow");
409 return FALSE;
410 } else
411 return TRUE;
412 case BD_CRYPTO_TECH_INTEGRITY:
413 ret = mode & (BD_CRYPTO_TECH_MODE_CREATE|BD_CRYPTO_TECH_MODE_OPEN_CLOSE|BD_CRYPTO_TECH_MODE_QUERY);
414 if (ret != mode) {
415 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
416 "Only 'create', 'open' and 'query' supported for Integrity");
417 return FALSE;
418 } else
419 return TRUE;
420 case BD_CRYPTO_TECH_BITLK:
421 ret = mode & (BD_CRYPTO_TECH_MODE_OPEN_CLOSE|BD_CRYPTO_TECH_MODE_QUERY);
422 if (ret != mode) {
423 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
424 "Only 'open' and 'query' supported for BITLK");
425 return FALSE;
426 } else
427 return TRUE;
428 case BD_CRYPTO_TECH_KEYRING:
429 ret = mode & BD_CRYPTO_TECH_MODE_ADD_KEY;
430 if (ret != mode) {
431 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
432 "Only 'add key' supported for kernel keyring");
433 return FALSE;
434 } else
435 return TRUE;
436 case BD_CRYPTO_TECH_FVAULT2:
437 #ifndef LIBCRYPTSETUP_26
438 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
439 "FVAULT2 technology requires libcryptsetup >= 2.6.0");
440 return FALSE;
441 #endif
442 ret = mode & BD_CRYPTO_TECH_MODE_OPEN_CLOSE;
443 if (ret != mode) {
444 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
445 "Only 'open' supported for FVAULT2");
446 return FALSE;
447 } else
448 return TRUE;
449 case BD_CRYPTO_TECH_SED_OPAL:
450 #if !defined(LIBCRYPTSETUP_27) || !defined(HAVE_LINUX_OPAL)
451 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
452 "OPAL technology requires libcryptsetup >= 2.7.0 and kernel with SED OPAL support");
453 return FALSE;
454 #endif
455 ret = mode & (BD_CRYPTO_TECH_MODE_CREATE|BD_CRYPTO_TECH_MODE_QUERY|BD_CRYPTO_TECH_MODE_MODIFY);
456 if (ret != mode) {
457 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
458 "Only 'create', 'query' and 'modify' supported for OPAL");
459 return FALSE;
460 } else
461 return TRUE;
462 default:
463 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL, "Unknown technology");
464 return FALSE;
465 }
466
467 return TRUE;
468 }
469
470 /**
471 * bd_crypto_error_quark: (skip)
472 */
473 GQuark bd_crypto_error_quark (void)
474 {
475 return g_quark_from_static_string ("g-bd-crypto-error-quark");
476 }
477
478 /**
479 * bd_crypto_generate_backup_passphrase:
480 * @error: (out) (optional): place to store error (if any)
481 *
482 * Returns: (transfer full): A newly generated %BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH-long passphrase.
483 *
484 * See %BD_CRYPTO_BACKUP_PASSPHRASE_CHARSET for the definition of the charset used for the passphrase.
485 *
486 * Tech category: always available
487 */
488 gchar* bd_crypto_generate_backup_passphrase (GError **error G_GNUC_UNUSED) {
489 guint8 i = 0;
490 guint8 offset = 0;
491 guint8 charset_length = strlen (BD_CRYPTO_BACKUP_PASSPHRASE_CHARSET);
492
493 /* passphrase with groups of 5 characters separated with dashes, plus a null terminator */
494 gchar *ret = g_new0 (gchar, BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH + (BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH / 5));
495
496 for (i=0; i < BD_CRYPTO_BACKUP_PASSPHRASE_LENGTH; i++) {
497 if (i > 0 && (i % 5 == 0)) {
498 /* put a dash between every 5 characters */
499 ret[i+offset] = '-';
500 offset++;
501 }
502 ret[i+offset] = BD_CRYPTO_BACKUP_PASSPHRASE_CHARSET[g_random_int_range (0, charset_length)];
503 }
504
505 return ret;
506 }
507
508 /**
509 * bd_crypto_device_is_luks:
510 * @device: the queried device
511 * @error: (out) (optional): place to store error (if any)
512 *
513 * Returns: %TRUE if the given @device is a LUKS device or %FALSE if not or
514 * failed to determine (the @error) is populated with the error in such
515 * cases)
516 *
517 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
518 */
519 gboolean bd_crypto_device_is_luks (const gchar *device, GError **error) {
520 blkid_probe probe = NULL;
521 gint fd = 0;
522 gint status = 0;
523 const gchar *value = NULL;
524 guint n_try = 0;
525
526 probe = blkid_new_probe ();
527 if (!probe) {
528 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
529 "Failed to create a new probe");
530 return FALSE;
531 }
532
533 fd = open (device, O_RDONLY|O_CLOEXEC);
534 if (fd == -1) {
535 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
536 "Failed to open the device '%s'", device);
537 blkid_free_probe (probe);
538 return FALSE;
539 }
540
541 /* we may need to try multiple times with some delays in case the device is
542 busy at the very moment */
543 for (n_try=5, status=-1; (status != 0) && (n_try > 0); n_try--) {
544 status = blkid_probe_set_device (probe, fd, 0, 0);
545 if (status != 0)
546 g_usleep (100 * 1000); /* microseconds */
547 }
548 if (status != 0) {
549 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
550 "Failed to create a probe for the device '%s'", device);
551 blkid_free_probe (probe);
552 close (fd);
553 return FALSE;
554 }
555
556 blkid_probe_enable_partitions (probe, 1);
557 blkid_probe_set_partitions_flags (probe, BLKID_PARTS_MAGIC);
558 blkid_probe_enable_superblocks (probe, 1);
559 blkid_probe_set_superblocks_flags (probe, BLKID_SUBLKS_USAGE | BLKID_SUBLKS_TYPE |
560 BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM);
561
562 /* we may need to try multiple times with some delays in case the device is
563 busy at the very moment */
564 for (n_try=5, status=-1; !(status == 0 || status == 1) && (n_try > 0); n_try--) {
565 status = blkid_do_safeprobe (probe);
566 if (status < 0)
567 g_usleep (100 * 1000); /* microseconds */
568 }
569 if (status < 0) {
570 /* -1 or -2 = error during probing*/
571 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
572 "Failed to probe the device '%s'", device);
573 blkid_free_probe (probe);
574 close (fd);
575 return FALSE;
576 } else if (status == 1) {
577 /* 1 = nothing detected */
578 blkid_free_probe (probe);
579 close (fd);
580 return FALSE;
581 }
582
583 status = blkid_probe_lookup_value (probe, "USAGE", &value, NULL);
584 if (status != 0) {
585 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
586 "Failed to get usage for the device '%s'", device);
587 blkid_free_probe (probe);
588 close (fd);
589 return FALSE;
590 }
591
592 if (g_strcmp0 (value, "crypto") != 0) {
593 blkid_free_probe (probe);
594 close (fd);
595 return FALSE;
596 }
597
598 status = blkid_probe_lookup_value (probe, "TYPE", &value, NULL);
599 if (status != 0) {
600 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
601 "Failed to get filesystem type for the device '%s'", device);
602 blkid_free_probe (probe);
603 close (fd);
604 return FALSE;
605 }
606
607 if (g_strcmp0 (value, "crypto_LUKS") != 0) {
608 blkid_free_probe (probe);
609 close (fd);
610 return FALSE;
611 }
612
613 blkid_free_probe (probe);
614 close (fd);
615
616 return TRUE;
617 }
618
619 /**
620 * bd_crypto_luks_status:
621 * @luks_device: the queried LUKS device
622 * @error: (out) (optional): place to store error (if any)
623 *
624 * Returns: (transfer none): one of "invalid", "inactive", "active" or "busy" or
625 * %NULL if failed to determine (@error is populated with the error in
626 * such cases)
627 *
628 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
629 */
630 const gchar* bd_crypto_luks_status (const gchar *luks_device, GError **error) {
631 struct crypt_device *cd = NULL;
632 gint ret_num;
633 const gchar *ret = NULL;
634 crypt_status_info status;
635
636 ret_num = crypt_init_by_name (&cd, luks_device);
637 if (ret_num != 0) {
638 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
639 "Failed to initialize device: %s", strerror_l (-ret_num, c_locale));
640 return NULL;
641 }
642
643 status = crypt_status (cd, luks_device);
644 switch (status) {
645 case CRYPT_INVALID:
646 ret = "invalid";
647 break;
648 case CRYPT_INACTIVE:
649 ret = "inactive";
650 break;
651 case CRYPT_ACTIVE:
652 ret = "active";
653 break;
654 case CRYPT_BUSY:
655 ret = "busy";
656 break;
657 default:
658 ret = NULL;
659 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_STATE,
660 "Unknown device's state");
661 }
662
663 crypt_free (cd);
664 return ret;
665 }
666
667 static struct crypt_pbkdf_type *get_pbkdf_params (BDCryptoLUKSPBKDF *user_pbkdf, GError **error) {
668 const struct crypt_pbkdf_type *default_pbkdf = NULL;
669 struct crypt_pbkdf_type *new_pbkdf = NULL;
670
671 if (user_pbkdf == NULL)
672 return NULL;
673
674 /* crypt_get_pbkdf_default returns default pbkdf parameters only based
675 on the luks version -- so for LUKS2 it returns default values for
676 argon2 but we also need to be able to provide default values if user
677 wants pbkdf2 and only specifies type -- we will use the defaults for
678 argon2 and ignore parameters specific to it
679 better API for this should be part of cryptsetup 2.0.4
680 */
681 default_pbkdf = crypt_get_pbkdf_default (CRYPT_LUKS2);
682 if (!default_pbkdf) {
683 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
684 "Failed to get default values for pbkdf.");
685 return NULL;
686 }
687
688 new_pbkdf = g_new0 (struct crypt_pbkdf_type, 1);
689
690 new_pbkdf->flags = default_pbkdf->flags;
691
692 if (user_pbkdf->type)
693 new_pbkdf->type = user_pbkdf->type;
694 else
695 new_pbkdf->type = default_pbkdf->type;
696
697 if (user_pbkdf->hash)
698 new_pbkdf->hash = user_pbkdf->hash;
699 else
700 new_pbkdf->hash = default_pbkdf->hash;
701
702 if (user_pbkdf->time_ms)
703 new_pbkdf->time_ms = user_pbkdf->time_ms;
704 else
705 new_pbkdf->time_ms = default_pbkdf->time_ms;
706
707 if (user_pbkdf->iterations) {
708 new_pbkdf->iterations = user_pbkdf->iterations;
709 /* iterations set manually -> do not run benchmark */
710 new_pbkdf->flags = CRYPT_PBKDF_NO_BENCHMARK;
711 } else
712 new_pbkdf->iterations = default_pbkdf->iterations;
713
714 /* 'max_memory_kb' and 'parallel_threads' are not used in pbkdf2 */
715 if (g_strcmp0 (user_pbkdf->type, CRYPT_KDF_PBKDF2) == 0) {
716 if (user_pbkdf->max_memory_kb)
717 bd_utils_log_format (LOG_WARNING, "'max_memory_kb' is not valid option for 'pbkdf2', ignoring.");
718
719 new_pbkdf->max_memory_kb = 0;
720 new_pbkdf->parallel_threads = 0;
721 } else {
722 if (user_pbkdf->max_memory_kb)
723 new_pbkdf->max_memory_kb = user_pbkdf->max_memory_kb;
724 else
725 new_pbkdf->max_memory_kb = default_pbkdf->max_memory_kb;
726
727 if (user_pbkdf->parallel_threads)
728 new_pbkdf->parallel_threads = user_pbkdf->parallel_threads;
729 else
730 new_pbkdf->parallel_threads = default_pbkdf->parallel_threads;
731 }
732
733 return new_pbkdf;
734 }
735
736 typedef enum {
737 BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_NONE = 0,
738 BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE,
739 BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE,
740 BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYRING,
741 BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_VOLUME_KEY,
742 } BDCryptoKeyslotContextType;
743
744 struct _BDCryptoKeyslotContext {
745 BDCryptoKeyslotContextType type;
746
747 union {
748 struct {
749 guint8 *pass_data;
750 gsize data_len;
751 } passphrase;
752
753 struct {
754 gchar *keyfile;
755 guint64 keyfile_offset;
756 gsize key_size;
757 } keyfile;
758
759 struct {
760 gchar *key_desc;
761 } keyring;
762
763 struct {
764 guint8 *volume_key;
765 gsize volume_key_size;
766 } volume_key;
767 } u;
768 };
769
770 void bd_crypto_keyslot_context_free (BDCryptoKeyslotContext *context) {
771 if (context == NULL)
772 return;
773
774 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
775 explicit_bzero (context->u.passphrase.pass_data, context->u.passphrase.data_len);
776 g_free (context->u.passphrase.pass_data);
777 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
778 g_free (context->u.keyfile.keyfile);
779 else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYRING)
780 g_free (context->u.keyring.key_desc);
781 else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_VOLUME_KEY) {
782 explicit_bzero (context->u.volume_key.volume_key, context->u.volume_key.volume_key_size);
783 g_free (context->u.volume_key.volume_key);
784 }
785
786 g_free (context);
787 }
788
789 BDCryptoKeyslotContext* bd_crypto_keyslot_context_copy (BDCryptoKeyslotContext *context) {
790 if (context == NULL)
791 return NULL;
792
793 BDCryptoKeyslotContext *new_context = g_new0 (BDCryptoKeyslotContext, 1);
794 new_context->type = context->type;
795
796 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
797 new_context->u.passphrase.pass_data = g_new0 (guint8, context->u.passphrase.data_len);
798 memcpy (new_context->u.passphrase.pass_data, context->u.passphrase.pass_data, context->u.passphrase.data_len);
799 new_context->u.passphrase.data_len = context->u.passphrase.data_len;
800 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
801 new_context->u.keyfile.keyfile = g_strdup (context->u.keyfile.keyfile);
802 new_context->u.keyfile.keyfile_offset = context->u.keyfile.keyfile_offset;
803 new_context->u.keyfile.key_size = context->u.keyfile.key_size;
804 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYRING)
805 new_context->u.keyring.key_desc = g_strdup (context->u.keyring.key_desc);
806 else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_VOLUME_KEY) {
807 new_context->u.volume_key.volume_key = g_new0 (guint8, context->u.volume_key.volume_key_size);
808 memcpy (new_context->u.volume_key.volume_key, context->u.volume_key.volume_key, context->u.volume_key.volume_key_size);
809 new_context->u.volume_key.volume_key_size = context->u.volume_key.volume_key_size;
810 }
811
812 return new_context;
813 }
814
815 /**
816 * bd_crypto_keyslot_context_new_passphrase:
817 * @pass_data: (array length=data_len): a passphrase for the new context (may contain arbitrary binary data)
818 * @data_len: length of the @pass_data buffer
819 * @error: (out) (optional): place to store error (if any)
820 *
821 * Returns (transfer full): new %BDCryptoKeyslotContext initialized by passphrase or
822 * %NULL in case of error
823 *
824 * Tech category: always available
825 */
826 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_passphrase (const guint8 *pass_data, gsize data_len, GError **error) {
827 BDCryptoKeyslotContext *context = NULL;
828
829 if (!pass_data || data_len == 0) {
830 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_NO_KEY,
831 "No passphrase specified.");
832 return NULL;
833 }
834
835 context = g_new0 (BDCryptoKeyslotContext, 1);
836
837 context->type = BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE;
838
839 context->u.passphrase.pass_data = g_new0 (guint8, data_len);
840 memcpy (context->u.passphrase.pass_data, pass_data, data_len);
841
842 context->u.passphrase.data_len = data_len;
843
844 return context;
845 }
846
847 /**
848 * bd_crypto_keyslot_context_new_keyfile:
849 * @keyfile: a key file for the new context
850 * @keyfile_offset: number of bytes to skip at start of @keyfile
851 * @key_size: number of bytes to read from @keyfile or 0 for unlimited
852 * @error: (out) (optional): place to store error (if any)
853 *
854 * Returns (transfer full): new %BDCryptoKeyslotContext initialized by key file or
855 * %NULL in case of error
856 *
857 * Tech category: always available
858 */
859 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_keyfile (const gchar *keyfile, guint64 keyfile_offset, gsize key_size, GError **error G_GNUC_UNUSED) {
860 BDCryptoKeyslotContext *context = NULL;
861
862 context = g_new0 (BDCryptoKeyslotContext, 1);
863
864 context->type = BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE;
865
866 context->u.keyfile.keyfile = g_strdup (keyfile);
867 context->u.keyfile.keyfile_offset = keyfile_offset;
868 context->u.keyfile.key_size = key_size;
869
870 return context;
871 }
872
873 /**
874 * bd_crypto_keyslot_context_new_keyring:
875 * @key_desc: kernel keyring key description
876 * @error: (out) (optional): place to store error (if any)
877 *
878 * Returns (transfer full): new %BDCryptoKeyslotContext initialized by @key_desc or
879 * %NULL in case of error
880 *
881 * Note: Keyslot passphrase must be stored in 'user' key type and the key has to be reachable
882 * by process context on behalf of which this function is called.
883 *
884 * Tech category: always available
885 */
886 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_keyring (const gchar *key_desc, GError **error G_GNUC_UNUSED) {
887 BDCryptoKeyslotContext *context = NULL;
888
889 context = g_new0 (BDCryptoKeyslotContext, 1);
890
891 context->type = BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYRING;
892
893 context->u.keyring.key_desc = g_strdup (key_desc);
894
895 return context;
896 }
897
898 /**
899 * bd_crypto_keyslot_context_new_volume_key:
900 * @volume_key: (array length=volume_key_size): a volume key for the new context (may contain arbitrary binary data)
901 * @volume_key_size: length of the @volume_key_size buffer
902 * @error: (out) (optional): place to store error (if any)
903 *
904 * Returns (transfer full): new %BDCryptoKeyslotContext initialized by volume key or
905 * %NULL in case of error
906 *
907 * Tech category: always available
908 */
909 BDCryptoKeyslotContext* bd_crypto_keyslot_context_new_volume_key (const guint8 *volume_key, gsize volume_key_size, GError **error) {
910 BDCryptoKeyslotContext *context = NULL;
911
912 if (!volume_key || volume_key_size == 0) {
913 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_NO_KEY,
914 "No volume key specified.");
915 return NULL;
916 }
917
918 context = g_new0 (BDCryptoKeyslotContext, 1);
919
920 context->type = BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_VOLUME_KEY;
921
922 context->u.volume_key.volume_key = g_new0 (guint8, volume_key_size);
923 memcpy (context->u.volume_key.volume_key, volume_key, volume_key_size);
924
925 context->u.volume_key.volume_key_size = volume_key_size;
926
927 return context;
928 }
929
930
931
932 gboolean _crypto_luks_format (const gchar *device,
933 const gchar *cipher,
934 guint64 key_size,
935 BDCryptoKeyslotContext *context,
936 guint64 min_entropy,
937 BDCryptoLUKSVersion luks_version,
938 BDCryptoLUKSExtra *extra,
939 BDCryptoLUKSHWEncryptionType hw_encryption,
940 #ifdef LIBCRYPTSETUP_27
941 BDCryptoKeyslotContext *opal_context,
942 #else
943 BDCryptoKeyslotContext *opal_context G_GNUC_UNUSED,
944 #endif
945 GError **error) {
946 struct crypt_device *cd = NULL;
947 gint ret;
948 gchar **cipher_specs = NULL;
949 guint32 current_entropy = 0;
950 gint dev_random_fd = -1;
951 gchar *key_buffer = NULL;
952 gsize buf_len = 0;
953 guint64 progress_id = 0;
954 gchar *msg = NULL;
955 const gchar* crypt_version = NULL;
956 GError *l_error = NULL;
957 struct crypt_pbkdf_type *pbkdf = NULL;
958
959 #ifdef LIBCRYPTSETUP_27
960 struct crypt_params_hw_opal opal_params = {
961 .user_key_size = DEFAULT_OPAL_KEYSIZE_BITS / 8
962 };
963
964 gboolean is_opal = (hw_encryption == BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_ONLY || hw_encryption == BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_AND_SW);
965 #endif
966
967 msg = g_strdup_printf ("Started formatting '%s' as LUKS device", device);
968 progress_id = bd_utils_report_started (msg);
969 g_free (msg);
970
971 if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS1)
972 crypt_version = CRYPT_LUKS1;
973 else if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS2)
974 crypt_version = CRYPT_LUKS2;
975 else {
976 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
977 "Unknown or unsupported LUKS version specified");
978 bd_utils_report_finished (progress_id, l_error->message);
979 g_propagate_error (error, l_error);
980 return FALSE;
981 }
982
983 ret = crypt_init (&cd, device);
984 if (ret != 0) {
985 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
986 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
987 bd_utils_report_finished (progress_id, l_error->message);
988 g_propagate_error (error, l_error);
989 return FALSE;
990 }
991
992 if (hw_encryption == BD_CRYPTO_LUKS_HW_ENCRYPTION_SW_ONLY || hw_encryption == BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_AND_SW) {
993 cipher = cipher ? cipher : DEFAULT_LUKS_CIPHER;
994 cipher_specs = g_strsplit (cipher, "-", 2);
995 if (g_strv_length (cipher_specs) != 2) {
996 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_SPEC,
997 "Invalid cipher specification: '%s'", cipher);
998 crypt_free (cd);
999 g_strfreev (cipher_specs);
1000 bd_utils_report_finished (progress_id, l_error->message);
1001 g_propagate_error (error, l_error);
1002 return FALSE;
1003 }
1004
1005 if (key_size == 0) {
1006 if (g_str_has_prefix (cipher_specs[1], "xts-"))
1007 key_size = DEFAULT_LUKS_KEYSIZE_BITS * 2;
1008 else
1009 key_size = DEFAULT_LUKS_KEYSIZE_BITS;
1010 }
1011 } else
1012 cipher_specs = g_new0 (gchar*, 2);
1013
1014 #ifdef LIBCRYPTSETUP_27
1015 if (is_opal)
1016 key_size += DEFAULT_OPAL_KEYSIZE_BITS;
1017 #endif
1018
1019 /* key_size should be in bytes */
1020 key_size = key_size / 8;
1021
1022 /* wait for enough random data entropy (if requested) */
1023 if (min_entropy > 0) {
1024 dev_random_fd = open ("/dev/random", O_RDONLY);
1025 if (dev_random_fd >= 0) {
1026 if (ioctl (dev_random_fd, RNDGETENTCNT, &current_entropy) < 0) {
1027 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
1028 "Failed to get random data entropy level: %s",
1029 strerror_l (errno, c_locale));
1030 close (dev_random_fd);
1031 crypt_free (cd);
1032 g_strfreev (cipher_specs);
1033 bd_utils_report_finished (progress_id, l_error->message);
1034 g_propagate_error (error, l_error);
1035 return FALSE;
1036 }
1037 while (current_entropy < min_entropy) {
1038 bd_utils_report_progress (progress_id, 0, "Waiting for enough random data entropy");
1039 sleep (1);
1040 if (ioctl (dev_random_fd, RNDGETENTCNT, &current_entropy) < 0) {
1041 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
1042 "Failed to get random data entropy level: %s",
1043 strerror_l (errno, c_locale));
1044 close (dev_random_fd);
1045 crypt_free (cd);
1046 g_strfreev (cipher_specs);
1047 bd_utils_report_finished (progress_id, l_error->message);
1048 g_propagate_error (error, l_error);
1049 return FALSE;
1050 }
1051 }
1052 close (dev_random_fd);
1053 } else {
1054 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
1055 "Failed to check random data entropy level");
1056 crypt_free (cd);
1057 g_strfreev (cipher_specs);
1058 bd_utils_report_finished (progress_id, l_error->message);
1059 g_propagate_error (error, l_error);
1060 return FALSE;
1061 }
1062 }
1063
1064 #ifdef LIBCRYPTSETUP_27
1065 if (is_opal) {
1066 if (opal_context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1067 opal_params.admin_key = (char *) opal_context->u.passphrase.pass_data;
1068 opal_params.admin_key_size = opal_context->u.passphrase.data_len;
1069 } else {
1070 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1071 "Only 'passphrase' context type is valid for OPAL format.");
1072 bd_utils_report_finished (progress_id, l_error->message);
1073 g_propagate_error (error, l_error);
1074 crypt_free (cd);
1075 g_strfreev (cipher_specs);
1076 return FALSE;
1077 }
1078 }
1079
1080 if (is_opal) {
1081 /* XXX: workaround for a bug in cryptsetup where crypt_format_luks2_opal doesn't
1082 * initialize crypto backend leading to an abort during the call, setting
1083 * the pbkdf parameters will run the initialization so just get the default
1084 * params and set them
1085 * See also: https://gitlab.com/cryptsetup/cryptsetup/-/commit/42f4a68705384eeecb5b31b71cc8a8fe19bca916
1086 */
1087 const struct crypt_pbkdf_type *pbkdf_default;
1088 pbkdf_default = crypt_get_pbkdf_default (CRYPT_LUKS2);
1089 crypt_set_pbkdf_type (cd, pbkdf_default);
1090 }
1091 #endif
1092
1093 if (extra) {
1094 if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS1) {
1095
1096 if (extra->integrity || extra->sector_size || extra->label || extra->subsystem) {
1097 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_PARAMS,
1098 "Invalid extra arguments specified. Only `data_alignment`"
1099 "and `data_device` are valid for LUKS 1.");
1100 crypt_free (cd);
1101 g_strfreev (cipher_specs);
1102 bd_utils_report_finished (progress_id, l_error->message);
1103 g_propagate_error (error, l_error);
1104 return FALSE;
1105 }
1106
1107 if (extra->pbkdf) {
1108 if (g_strcmp0 (extra->pbkdf->type, "pbkdf2") != 0) {
1109 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_PARAMS,
1110 "Invalid pbkdf specified. Only `pbkdf2` is valid for LUKS 1.");
1111 crypt_free (cd);
1112 g_strfreev (cipher_specs);
1113 bd_utils_report_finished (progress_id, l_error->message);
1114 g_propagate_error (error, l_error);
1115 return FALSE;
1116 }
1117
1118 pbkdf = get_pbkdf_params (extra->pbkdf, &l_error);
1119 if (pbkdf == NULL && l_error != NULL) {
1120 crypt_free (cd);
1121 g_strfreev (cipher_specs);
1122 bd_utils_report_finished (progress_id, l_error->message);
1123 g_propagate_prefixed_error (error, l_error,
1124 "Failed to get PBKDF parameters for '%s'.", device);
1125 return FALSE;
1126 }
1127 crypt_set_pbkdf_type (cd, pbkdf);
1128 }
1129
1130 struct crypt_params_luks1 params = ZERO_INIT;
1131 params.data_alignment = extra->data_alignment;
1132 params.data_device = extra->data_device;
1133 ret = crypt_format (cd, crypt_version, cipher_specs[0], cipher_specs[1],
1134 NULL, NULL, key_size, &params);
1135 g_free (pbkdf);
1136 }
1137 else if (luks_version == BD_CRYPTO_LUKS_VERSION_LUKS2) {
1138 struct crypt_params_luks2 params = ZERO_INIT;
1139 struct crypt_pbkdf_type *pbkdf = get_pbkdf_params (extra->pbkdf, &l_error);
1140
1141 if (pbkdf == NULL && l_error != NULL) {
1142 crypt_free (cd);
1143 g_strfreev (cipher_specs);
1144 bd_utils_report_finished (progress_id, l_error->message);
1145 g_propagate_prefixed_error (error, l_error,
1146 "Failed to get PBKDF parameters for '%s'.", device);
1147 return FALSE;
1148 }
1149
1150 params.pbkdf = pbkdf;
1151 params.integrity = extra->integrity;
1152 params.integrity_params = NULL;
1153 params.data_alignment = extra->data_alignment;
1154 params.data_device = extra->data_device;
1155 params.sector_size = extra->sector_size ? extra->sector_size : DEFAULT_LUKS2_SECTOR_SIZE;
1156 params.label = extra->label;
1157 params.subsystem = extra->subsystem;
1158
1159 #ifdef LIBCRYPTSETUP_27
1160 if (is_opal)
1161 ret = crypt_format_luks2_opal (cd, cipher_specs[0], cipher_specs[1],
1162 NULL, NULL, key_size, &params, &opal_params);
1163 else
1164 #endif
1165 ret = crypt_format (cd, crypt_version, cipher_specs[0], cipher_specs[1],
1166 NULL, NULL, key_size, &params);
1167 g_free (pbkdf);
1168 }
1169 } else {
1170 #ifdef LIBCRYPTSETUP_27
1171 if (is_opal) {
1172 struct crypt_params_luks2 params = ZERO_INIT;
1173 params.pbkdf = crypt_get_pbkdf_default (CRYPT_LUKS2);
1174 ret = crypt_format_luks2_opal (cd, cipher_specs[0], cipher_specs[1],
1175 NULL, NULL, key_size, &params, &opal_params);
1176 } else
1177 #endif
1178 ret = crypt_format (cd, crypt_version, cipher_specs[0], cipher_specs[1],
1179 NULL, NULL, key_size, NULL);
1180 }
1181 g_strfreev (cipher_specs);
1182
1183 if (ret != 0) {
1184 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
1185 "Failed to format device: %s", strerror_l (-ret, c_locale));
1186 crypt_free (cd);
1187 bd_utils_report_finished (progress_id, l_error->message);
1188 g_propagate_error (error, l_error);
1189 return FALSE;
1190 }
1191
1192 bd_utils_report_progress (progress_id, 50, "Format created");
1193 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1194 ret = crypt_keyslot_add_by_volume_key (cd, CRYPT_ANY_SLOT, NULL, 0,
1195 (const char *) context->u.passphrase.pass_data,
1196 context->u.passphrase.data_len);
1197 if (ret < 0) {
1198 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
1199 "Failed to add passphrase: %s", strerror_l (-ret, c_locale));
1200 crypt_free (cd);
1201 bd_utils_report_finished (progress_id, l_error->message);
1202 g_propagate_error (error, l_error);
1203 return FALSE;
1204 }
1205 bd_utils_report_progress (progress_id, 100, "Added key");
1206 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1207 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buffer, &buf_len,
1208 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
1209 if (ret != 0) {
1210 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1211 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile,
1212 strerror_l (-ret, c_locale));
1213 crypt_free (cd);
1214 bd_utils_report_finished (progress_id, l_error->message);
1215 g_propagate_error (error, l_error);
1216 return FALSE;
1217 }
1218 ret = crypt_keyslot_add_by_volume_key (cd, CRYPT_ANY_SLOT, NULL, 0,
1219 (const char*) key_buffer, buf_len);
1220 crypt_safe_free (key_buffer);
1221 if (ret < 0) {
1222 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
1223 "Failed to add key file: %s", strerror_l (-ret, c_locale));
1224 crypt_free (cd);
1225 bd_utils_report_finished (progress_id, l_error->message);
1226 g_propagate_error (error, l_error);
1227 return FALSE;
1228 }
1229 bd_utils_report_progress (progress_id, 100, "Added key");
1230 } else {
1231 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1232 "Only 'passphrase' and 'key file' context types are valid for LUKS format.");
1233 bd_utils_report_finished (progress_id, l_error->message);
1234 g_propagate_error (error, l_error);
1235 crypt_free (cd);
1236 return FALSE;
1237 }
1238
1239 crypt_free (cd);
1240
1241 bd_utils_report_finished (progress_id, "Completed");
1242 return TRUE;
1243 }
1244
1245 /**
1246 * bd_crypto_luks_format:
1247 * @device: a device to format as LUKS
1248 * @cipher: (nullable): cipher specification (type-mode, e.g. "aes-xts-plain64") or %NULL to use the default
1249 * @key_size: size of the volume key in bits or 0 to use the default
1250 * @context: key slot context (passphrase/keyfile/token...) for this LUKS device
1251 * @min_entropy: minimum random data entropy (in bits) required to format @device as LUKS
1252 * @luks_version: whether to use LUKS v1 or LUKS v2
1253 * @extra: (nullable): extra arguments for LUKS format creation
1254 * @error: (out) (optional): place to store error (if any)
1255 *
1256 * Formats the given @device as LUKS according to the other parameters given. If
1257 * @min_entropy is specified (greater than 0), the function waits for enough
1258 * entropy to be available in the random data pool (WHICH MAY POTENTIALLY TAKE
1259 * FOREVER).
1260 *
1261 * Supported @context types for this function: passphrase, key file
1262 *
1263 * Returns: whether the given @device was successfully formatted as LUKS or not
1264 * (the @error) contains the error in such cases)
1265 *
1266 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_CREATE
1267 */
1268 gboolean bd_crypto_luks_format (const gchar *device, const gchar *cipher, guint64 key_size, BDCryptoKeyslotContext *context, guint64 min_entropy, BDCryptoLUKSVersion luks_version, BDCryptoLUKSExtra *extra,GError **error) {
1269 return _crypto_luks_format (device, cipher, key_size, context, min_entropy, luks_version, extra, BD_CRYPTO_LUKS_HW_ENCRYPTION_SW_ONLY, NULL, error);
1270 }
1271
1272 static gboolean _is_dm_name_valid (const gchar *name, GError **error) {
1273 if (strlen (name) >= 128) {
1274 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_PARAMS,
1275 "Device name can be at most 127 characters long.");
1276 return FALSE;
1277 }
1278
1279 if (strchr (name, '/')) {
1280 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_PARAMS,
1281 "Device name cannot contain '/' character.");
1282 return FALSE;
1283 }
1284
1285 return TRUE;
1286 }
1287
1288 /**
1289 * bd_crypto_luks_open_flags:
1290 * @device: the device to open
1291 * @name: name for the LUKS device
1292 * @context: key slot context (passphrase/keyfile/token...) to open this LUKS @device
1293 * @flags: activation flags for the LUKS device
1294 * @error: (out) (optional): place to store error (if any)
1295 *
1296 * Supported @context types for this function: passphrase, key file, keyring
1297 *
1298 * Returns: whether the @device was successfully opened or not
1299 *
1300 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
1301 *
1302 * Example of using %bd_crypto_luks_open_flags with %BDCryptoKeyslotContext:
1303 *
1304 * |[<!-- language="C" -->
1305 * BDCryptoKeyslotContext *context = NULL;
1306 *
1307 * context = bd_crypto_keyslot_context_new_passphrase ("passphrase", 10, NULL);
1308 * bd_crypto_luks_open_flags ("/dev/vda1", "luks-device", context, 0, NULL);
1309 * ]|
1310 */
1311 gboolean bd_crypto_luks_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, BDCryptoOpenFlags flags, GError **error) {
1312 struct crypt_device *cd = NULL;
1313 gchar *key_buffer = NULL;
1314 gsize buf_len = 0;
1315 gint ret = 0;
1316 guint64 progress_id = 0;
1317 gchar *msg = NULL;
1318 GError *l_error = NULL;
1319 guint32 crypt_flags = 0;
1320
1321 if (!_is_dm_name_valid (name, error))
1322 return FALSE;
1323
1324 msg = g_strdup_printf ("Started opening '%s' LUKS device", device);
1325 progress_id = bd_utils_report_started (msg);
1326 g_free (msg);
1327
1328 ret = crypt_init (&cd, device);
1329 if (ret != 0) {
1330 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1331 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
1332 bd_utils_report_finished (progress_id, l_error->message);
1333 g_propagate_error (error, l_error);
1334 return FALSE;
1335 }
1336
1337 ret = crypt_load (cd, CRYPT_LUKS, NULL);
1338 if (ret != 0) {
1339 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1340 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
1341 crypt_free (cd);
1342 bd_utils_report_finished (progress_id, l_error->message);
1343 g_propagate_error (error, l_error);
1344 return FALSE;
1345 }
1346
1347 if (flags & BD_CRYPTO_OPEN_ALLOW_DISCARDS)
1348 crypt_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
1349 if (flags & BD_CRYPTO_OPEN_READONLY)
1350 crypt_flags |= CRYPT_ACTIVATE_READONLY;
1351
1352 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1353 ret = crypt_activate_by_passphrase (cd, name, CRYPT_ANY_SLOT,
1354 (const char *) context->u.passphrase.pass_data,
1355 context->u.passphrase.data_len,
1356 crypt_flags);
1357 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1358 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buffer, &buf_len,
1359 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
1360 if (ret != 0) {
1361 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1362 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile, strerror_l (-ret, c_locale));
1363 crypt_free (cd);
1364 bd_utils_report_finished (progress_id, l_error->message);
1365 g_propagate_error (error, l_error);
1366 return FALSE;
1367 }
1368 ret = crypt_activate_by_passphrase (cd, name, CRYPT_ANY_SLOT, key_buffer, buf_len, crypt_flags);
1369 crypt_safe_free (key_buffer);
1370 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYRING)
1371 ret = crypt_activate_by_keyring (cd, name, context->u.keyring.key_desc, CRYPT_ANY_SLOT, crypt_flags);
1372 else {
1373 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1374 "Only 'passphrase', 'key file' and 'keyring' context types are valid for LUKS open.");
1375 bd_utils_report_finished (progress_id, l_error->message);
1376 g_propagate_error (error, l_error);
1377 crypt_free (cd);
1378 return FALSE;
1379 }
1380
1381 if (ret < 0) {
1382 if (ret == -EPERM)
1383 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1384 "Failed to activate device: Incorrect passphrase.");
1385 else if (ret == -ETXTBSY)
1386 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1387 "Failed to activate device: Unknown or unsupported LUKS2 requirements detected.");
1388 else
1389 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1390 "Failed to activate device: %s", strerror_l (-ret, c_locale));
1391
1392 crypt_free (cd);
1393 bd_utils_report_finished (progress_id, l_error->message);
1394 g_propagate_error (error, l_error);
1395 return FALSE;
1396 }
1397
1398 crypt_free (cd);
1399 bd_utils_report_finished (progress_id, "Completed");
1400 return TRUE;
1401 }
1402
1403 /**
1404 * bd_crypto_luks_open:
1405 * @device: the device to open
1406 * @name: name for the LUKS device
1407 * @context: key slot context (passphrase/keyfile/token...) to open this LUKS @device
1408 * @read_only: whether to open as read-only or not (meaning read-write)
1409 * @error: (out) (optional): place to store error (if any)
1410 *
1411 * Supported @context types for this function: passphrase, key file, keyring
1412 *
1413 * Returns: whether the @device was successfully opened or not
1414 *
1415 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
1416 *
1417 * Example of using %bd_crypto_luks_open with %BDCryptoKeyslotContext:
1418 *
1419 * |[<!-- language="C" -->
1420 * BDCryptoKeyslotContext *context = NULL;
1421 *
1422 * context = bd_crypto_keyslot_context_new_passphrase ("passphrase", 10, NULL);
1423 * bd_crypto_luks_open ("/dev/vda1", "luks-device", context, FALSE, NULL);
1424 * ]|
1425 */
1426 gboolean bd_crypto_luks_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, gboolean read_only, GError **error) {
1427 return bd_crypto_luks_open_flags (device, name, context, read_only ? BD_CRYPTO_OPEN_READONLY : 0, error);
1428 }
1429
1430 static gboolean _crypto_close (const gchar *device, const gchar *tech_name, GError **error) {
1431 struct crypt_device *cd = NULL;
1432 gint ret = 0;
1433 guint64 progress_id = 0;
1434 gchar *msg = NULL;
1435 GError *l_error = NULL;
1436
1437 msg = g_strdup_printf ("Started closing %s device '%s'", tech_name, device);
1438 progress_id = bd_utils_report_started (msg);
1439 g_free (msg);
1440
1441 ret = crypt_init_by_name (&cd, device);
1442 if (ret != 0) {
1443 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1444 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
1445 crypt_free (cd);
1446 bd_utils_report_finished (progress_id, l_error->message);
1447 g_propagate_error (error, l_error);
1448 return FALSE;
1449 }
1450
1451 ret = crypt_deactivate (cd, device);
1452 if (ret != 0) {
1453 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1454 "Failed to deactivate device: %s", strerror_l (-ret, c_locale));
1455 crypt_free (cd);
1456 bd_utils_report_finished (progress_id, l_error->message);
1457 g_propagate_error (error, l_error);
1458 return FALSE;
1459 }
1460
1461 crypt_free (cd);
1462 bd_utils_report_finished (progress_id, "Completed");
1463 return TRUE;
1464 }
1465
1466 /**
1467 * bd_crypto_luks_close:
1468 * @luks_device: LUKS device to close
1469 * @error: (out) (optional): place to store error (if any)
1470 *
1471 * Returns: whether the given @luks_device was successfully closed or not
1472 *
1473 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
1474 */
1475 gboolean bd_crypto_luks_close (const gchar *luks_device, GError **error) {
1476 return _crypto_close (luks_device, "LUKS", error);
1477 }
1478
1479 /**
1480 * bd_crypto_luks_add_key:
1481 * @device: device to add new key to
1482 * @context: key slot context (passphrase/keyfile/token...) for this LUKS @device
1483 * @ncontext: new key slot context (passphrase/keyfile/token...) to add to this LUKS @device
1484 * @error: (out) (optional): place to store error (if any)
1485 *
1486 * Supported @context types for this function: passphrase, key file
1487 *
1488 * Returns: whether the @ncontext was successfully added to @device
1489 * or not
1490 *
1491 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_ADD_KEY
1492 */
1493 gboolean bd_crypto_luks_add_key (const gchar *device, BDCryptoKeyslotContext *context, BDCryptoKeyslotContext *ncontext, GError **error) {
1494 gchar *key_buf = NULL;
1495 gsize buf_len = 0;
1496 gchar *nkey_buf = NULL;
1497 gsize nbuf_len = 0;
1498 struct crypt_device *cd = NULL;
1499 gint ret = 0;
1500 guint64 progress_id = 0;
1501 GError *l_error = NULL;
1502 gchar *msg = NULL;
1503
1504 msg = g_strdup_printf ("Started adding key to the LUKS device '%s'", device);
1505 progress_id = bd_utils_report_started (msg);
1506 g_free (msg);
1507
1508 ret = crypt_init (&cd, device);
1509 if (ret != 0) {
1510 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1511 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
1512 bd_utils_report_finished (progress_id, l_error->message);
1513 g_propagate_error (error, l_error);
1514 return FALSE;
1515 }
1516
1517 ret = crypt_load (cd, CRYPT_LUKS, NULL);
1518 if (ret != 0) {
1519 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1520 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
1521 crypt_free (cd);
1522 bd_utils_report_finished (progress_id, l_error->message);
1523 g_propagate_error (error, l_error);
1524 return FALSE;
1525 }
1526
1527 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1528 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buf, &buf_len,
1529 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
1530 if (ret != 0) {
1531 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1532 "Failed to load key from file '%s': %s", context->u.keyfile.keyfile,
1533 strerror_l (-ret, c_locale));
1534 crypt_free (cd);
1535 bd_utils_report_finished (progress_id, l_error->message);
1536 g_propagate_error (error, l_error);
1537 return FALSE;
1538 }
1539 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1540 key_buf = (char *) context->u.passphrase.pass_data;
1541 buf_len = context->u.passphrase.data_len;
1542 } else {
1543 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1544 "Only 'passphrase' and 'key file' context types are valid for LUKS add key.");
1545 bd_utils_report_finished (progress_id, l_error->message);
1546 g_propagate_error (error, l_error);
1547 crypt_free (cd);
1548 return FALSE;
1549 }
1550
1551 if (ncontext->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1552 ret = crypt_keyfile_device_read (cd, ncontext->u.keyfile.keyfile, &nkey_buf, &nbuf_len,
1553 ncontext->u.keyfile.keyfile_offset, ncontext->u.keyfile.key_size, 0);
1554 if (ret != 0) {
1555 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1556 "Failed to load key from file '%s': %s", ncontext->u.keyfile.keyfile,
1557 strerror_l (-ret, c_locale));
1558 crypt_free (cd);
1559 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1560 crypt_safe_free (key_buf);
1561 bd_utils_report_finished (progress_id, l_error->message);
1562 g_propagate_error (error, l_error);
1563 return FALSE;
1564 }
1565 } else if (ncontext->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1566 nkey_buf = (char *) ncontext->u.passphrase.pass_data;
1567 nbuf_len = ncontext->u.passphrase.data_len;
1568 } else {
1569 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1570 "Only 'passphrase' and 'key file' context types are valid for LUKS add key.");
1571 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1572 crypt_safe_free (key_buf);
1573 bd_utils_report_finished (progress_id, l_error->message);
1574 g_propagate_error (error, l_error);
1575 crypt_free (cd);
1576 return FALSE;
1577 }
1578
1579 ret = crypt_keyslot_add_by_passphrase (cd, CRYPT_ANY_SLOT, key_buf, buf_len, nkey_buf, nbuf_len);
1580
1581 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1582 crypt_safe_free (key_buf);
1583 if (ncontext->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1584 crypt_safe_free (nkey_buf);
1585 crypt_free (cd);
1586
1587 if (ret < 0) {
1588 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
1589 "Failed to add key: %s", strerror_l (-ret, c_locale));
1590 bd_utils_report_finished (progress_id, l_error->message);
1591 g_propagate_error (error, l_error);
1592 return FALSE;
1593 }
1594
1595 bd_utils_report_finished (progress_id, "Completed");
1596 return TRUE;
1597 }
1598
1599 /**
1600 * bd_crypto_luks_remove_key:
1601 * @device: device to add new key to
1602 * @context: key slot context (passphrase/keyfile/token...) to remove from this LUKS @device
1603 * @error: (out) (optional): place to store error (if any)
1604 *
1605 * Supported @context types for this function: passphrase, key file
1606 *
1607 * Returns: whether the key was successfully removed or not
1608 *
1609 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_REMOVE_KEY
1610 */
1611 gboolean bd_crypto_luks_remove_key (const gchar *device, BDCryptoKeyslotContext *context, GError **error) {
1612 struct crypt_device *cd = NULL;
1613 gint ret = 0;
1614 guint64 progress_id = 0;
1615 gchar *msg = NULL;
1616 gchar *key_buf = NULL;
1617 gsize buf_len = 0;
1618 GError *l_error = NULL;
1619
1620 msg = g_strdup_printf ("Started removing key from the LUKS device '%s'", device);
1621 progress_id = bd_utils_report_started (msg);
1622 g_free (msg);
1623
1624 ret = crypt_init (&cd, device);
1625 if (ret != 0) {
1626 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1627 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
1628 bd_utils_report_finished (progress_id, l_error->message);
1629 g_propagate_error (error, l_error);
1630 return FALSE;
1631 }
1632
1633 ret = crypt_load (cd, CRYPT_LUKS, NULL);
1634 if (ret != 0) {
1635 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1636 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
1637 crypt_free (cd);
1638 bd_utils_report_finished (progress_id, l_error->message);
1639 g_propagate_error (error, l_error);
1640 return FALSE;
1641 }
1642
1643 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1644 ret = crypt_activate_by_passphrase (cd, NULL, CRYPT_ANY_SLOT,
1645 (const char *) context->u.passphrase.pass_data,
1646 context->u.passphrase.data_len, 0);
1647 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1648 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buf, &buf_len,
1649 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
1650 if (ret != 0) {
1651 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1652 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile, strerror_l (-ret, c_locale));
1653 crypt_free (cd);
1654 bd_utils_report_finished (progress_id, l_error->message);
1655 g_propagate_error (error, l_error);
1656 return FALSE;
1657 }
1658 ret = crypt_activate_by_passphrase (cd, NULL, CRYPT_ANY_SLOT, key_buf, buf_len, 0);
1659 crypt_safe_free (key_buf);
1660 } else {
1661 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1662 "Only 'passphrase' and 'key file' context types are valid for LUKS remove key.");
1663 bd_utils_report_finished (progress_id, l_error->message);
1664 g_propagate_error (error, l_error);
1665 crypt_free (cd);
1666 return FALSE;
1667 }
1668
1669 if (ret < 0) {
1670 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEY_SLOT,
1671 "Failed to determine key slot: %s", strerror_l (-ret, c_locale));
1672 crypt_free (cd);
1673 bd_utils_report_finished (progress_id, l_error->message);
1674 g_propagate_error (error, l_error);
1675 return FALSE;
1676 }
1677
1678 ret = crypt_keyslot_destroy (cd, ret);
1679 if (ret != 0) {
1680 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_REMOVE_KEY,
1681 "Failed to remove key: %s", strerror_l (-ret, c_locale));
1682 crypt_free (cd);
1683 bd_utils_report_finished (progress_id, l_error->message);
1684 g_propagate_error (error, l_error);
1685 return FALSE;
1686 }
1687
1688 crypt_free (cd);
1689 bd_utils_report_finished (progress_id, "Completed");
1690 return TRUE;
1691 }
1692
1693 /**
1694 * bd_crypto_luks_change_key:
1695 * @device: device to change key of
1696 * @context: key slot context (passphrase/keyfile/token...) for this LUKS @device
1697 * @ncontext: new key slot context (passphrase/keyfile/token...) to add to this LUKS @device
1698 * @error: (out) (optional): place to store error (if any)
1699 *
1700 * Supported @context types for this function: passphrase, key file
1701 *
1702 * Returns: whether the key was successfully changed or not
1703 *
1704 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_ADD_KEY&%BD_CRYPTO_TECH_MODE_REMOVE_KEY
1705 */
1706 gboolean bd_crypto_luks_change_key (const gchar *device, BDCryptoKeyslotContext *context, BDCryptoKeyslotContext *ncontext, GError **error) {
1707 struct crypt_device *cd = NULL;
1708 gchar *key_buf = NULL;
1709 gsize buf_len = 0;
1710 gchar *nkey_buf = NULL;
1711 gsize nbuf_len = 0;
1712 gint ret = 0;
1713 guint64 progress_id = 0;
1714 gchar *msg = NULL;
1715 GError *l_error = NULL;
1716
1717 msg = g_strdup_printf ("Started changing key on the LUKS device '%s'", device);
1718 progress_id = bd_utils_report_started (msg);
1719 g_free (msg);
1720
1721 ret = crypt_init (&cd, device);
1722 if (ret != 0) {
1723 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1724 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
1725 bd_utils_report_finished (progress_id, l_error->message);
1726 g_propagate_error (error, l_error);
1727 return FALSE;
1728 }
1729
1730 ret = crypt_load (cd, CRYPT_LUKS, NULL);
1731 if (ret != 0) {
1732 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1733 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
1734 crypt_free (cd);
1735 bd_utils_report_finished (progress_id, l_error->message);
1736 g_propagate_error (error, l_error);
1737 return FALSE;
1738 }
1739
1740 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1741 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buf, &buf_len,
1742 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
1743 if (ret != 0) {
1744 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1745 "Failed to load key from file '%s': %s", context->u.keyfile.keyfile,
1746 strerror_l (-ret, c_locale));
1747 crypt_free (cd);
1748 bd_utils_report_finished (progress_id, l_error->message);
1749 g_propagate_error (error, l_error);
1750 return FALSE;
1751 }
1752 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1753 key_buf = (char *) context->u.passphrase.pass_data;
1754 buf_len = context->u.passphrase.data_len;
1755 } else {
1756 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1757 "Only 'passphrase' and 'key file' context types are valid for LUKS change key.");
1758 bd_utils_report_finished (progress_id, l_error->message);
1759 g_propagate_error (error, l_error);
1760 crypt_free (cd);
1761 return FALSE;
1762 }
1763
1764 if (ncontext->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1765 ret = crypt_keyfile_device_read (cd, ncontext->u.keyfile.keyfile, &nkey_buf, &nbuf_len,
1766 ncontext->u.keyfile.keyfile_offset, ncontext->u.keyfile.key_size, 0);
1767 if (ret != 0) {
1768 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1769 "Failed to load key from file '%s': %s", ncontext->u.keyfile.keyfile,
1770 strerror_l (-ret, c_locale));
1771 crypt_free (cd);
1772 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1773 crypt_safe_free (key_buf);
1774 bd_utils_report_finished (progress_id, l_error->message);
1775 g_propagate_error (error, l_error);
1776 return FALSE;
1777 }
1778 } else if (ncontext->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1779 nkey_buf = (char *) ncontext->u.passphrase.pass_data;
1780 nbuf_len = ncontext->u.passphrase.data_len;
1781 } else {
1782 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1783 "Only 'passphrase' and 'key file' context types are valid for LUKS change key.");
1784 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1785 crypt_safe_free (key_buf);
1786 bd_utils_report_finished (progress_id, l_error->message);
1787 g_propagate_error (error, l_error);
1788 crypt_free (cd);
1789 return FALSE;
1790 }
1791
1792 ret = crypt_keyslot_change_by_passphrase (cd, CRYPT_ANY_SLOT, CRYPT_ANY_SLOT,
1793 key_buf, buf_len, nkey_buf, nbuf_len);
1794
1795 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1796 crypt_safe_free (key_buf);
1797 if (ncontext->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
1798 crypt_safe_free (nkey_buf);
1799 crypt_free (cd);
1800
1801 if (ret < 0) {
1802 if (ret == -EPERM)
1803 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1804 "Failed to change the passphrase: No keyslot with given passphrase found.");
1805 else
1806 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ADD_KEY,
1807 "Failed to change the passphrase: %s", strerror_l (-ret, c_locale));
1808 bd_utils_report_finished (progress_id, l_error->message);
1809 g_propagate_error (error, l_error);
1810 return FALSE;
1811 }
1812
1813 bd_utils_report_finished (progress_id, "Completed");
1814 return TRUE;
1815 }
1816
1817 /**
1818 * bd_crypto_luks_resize:
1819 * @luks_device: opened LUKS device to resize
1820 * @size: requested size in sectors or 0 to adapt to the backing device
1821 * @context: (nullable): key slot context (passphrase/keyfile/token...) for this LUKS @device
1822 * @error: (out) (optional): place to store error (if any)
1823 *
1824 * Supported @context types for this function: passphrase, key file
1825 *
1826 * Returns: whether the @luks_device was successfully resized or not
1827 *
1828 * You need to specify either @context for LUKS 2 devices that
1829 * don't have verified key loaded in kernel.
1830 * For LUKS 1 devices you can set @context %NULL.
1831 *
1832 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_RESIZE
1833 */
1834 gboolean bd_crypto_luks_resize (const gchar *luks_device, guint64 size, BDCryptoKeyslotContext *context, GError **error) {
1835 struct crypt_device *cd = NULL;
1836 struct crypt_active_device cad;
1837 gint ret = 0;
1838 guint64 progress_id = 0;
1839 gchar *msg = NULL;
1840 gchar *key_buffer = NULL;
1841 gsize buf_len = 0;
1842 GError *l_error = NULL;
1843
1844 msg = g_strdup_printf ("Started resizing LUKS device '%s'", luks_device);
1845 progress_id = bd_utils_report_started (msg);
1846 g_free (msg);
1847
1848 ret = crypt_init_by_name (&cd, luks_device);
1849 if (ret != 0) {
1850 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1851 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
1852 crypt_free (cd);
1853 bd_utils_report_finished (progress_id, l_error->message);
1854 g_propagate_error (error, l_error);
1855 return FALSE;
1856 }
1857
1858 ret = crypt_get_active_device (cd, luks_device, &cad);
1859 if (ret != 0) {
1860 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1861 "Failed to get information about '%s': %s",
1862 luks_device, strerror_l (-ret, c_locale));
1863 crypt_free (cd);
1864 bd_utils_report_finished (progress_id, l_error->message);
1865 g_propagate_error (error, l_error);
1866 return FALSE;
1867 }
1868
1869 if (context) {
1870 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
1871 ret = crypt_activate_by_passphrase (cd, NULL, CRYPT_ANY_SLOT,
1872 (const char *) context->u.passphrase.pass_data,
1873 context->u.passphrase.data_len,
1874 cad.flags & CRYPT_ACTIVATE_KEYRING_KEY);
1875 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
1876 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buffer, &buf_len,
1877 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
1878 if (ret != 0) {
1879 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
1880 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile, strerror_l (-ret, c_locale));
1881 crypt_free (cd);
1882 bd_utils_report_finished (progress_id, l_error->message);
1883 g_propagate_error (error, l_error);
1884 return FALSE;
1885 }
1886 ret = crypt_activate_by_passphrase (cd, NULL, CRYPT_ANY_SLOT, key_buffer, buf_len,
1887 cad.flags & CRYPT_ACTIVATE_KEYRING_KEY);
1888 crypt_safe_free (key_buffer);
1889 } else {
1890 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
1891 "Only 'passphrase' and 'key file' context types are valid for LUKS resize.");
1892 bd_utils_report_finished (progress_id, l_error->message);
1893 g_propagate_error (error, l_error);
1894 crypt_free (cd);
1895 return FALSE;
1896 }
1897
1898 if (ret < 0) {
1899 if (ret == -EPERM)
1900 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1901 "Failed to activate device: Incorrect passphrase.");
1902 else
1903 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1904 "Failed to activate device: %s", strerror_l (-ret, c_locale));
1905 crypt_free (cd);
1906 bd_utils_report_finished (progress_id, l_error->message);
1907 g_propagate_error (error, l_error);
1908 return FALSE;
1909 }
1910 }
1911
1912 ret = crypt_resize (cd, luks_device, size);
1913 if (ret != 0) {
1914 if (ret == -EPERM && g_strcmp0 (crypt_get_type (cd), CRYPT_LUKS2) == 0) {
1915 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_RESIZE_PERM,
1916 "Insufficient permissions to resize device. You need to specify"
1917 " passphrase or keyfile to resize LUKS 2 devices that don't"
1918 " have verified key loaded in kernel.");
1919 crypt_free (cd);
1920 bd_utils_report_finished (progress_id, l_error->message);
1921 g_propagate_error (error, l_error);
1922 return FALSE;
1923
1924 }
1925 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_RESIZE_FAILED,
1926 "Failed to resize device: %s", strerror_l (-ret, c_locale));
1927 crypt_free (cd);
1928 bd_utils_report_finished (progress_id, l_error->message);
1929 g_propagate_error (error, l_error);
1930 return FALSE;
1931 }
1932
1933 crypt_free (cd);
1934 bd_utils_report_finished (progress_id, "Completed");
1935 return TRUE;
1936 }
1937
1938 /**
1939 * bd_crypto_luks_suspend:
1940 * @luks_device: LUKS device to suspend
1941 * @error: (out) (optional): place to store error (if any)
1942 *
1943 * Returns: whether the given @luks_device was successfully suspended or not
1944 *
1945 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_SUSPEND_RESUME
1946 */
1947 gboolean bd_crypto_luks_suspend (const gchar *luks_device, GError **error) {
1948 struct crypt_device *cd = NULL;
1949 gint ret = 0;
1950 guint64 progress_id = 0;
1951 gchar *msg = NULL;
1952 GError *l_error = NULL;
1953
1954 msg = g_strdup_printf ("Started suspending LUKS device '%s'", luks_device);
1955 progress_id = bd_utils_report_started (msg);
1956 g_free (msg);
1957
1958 ret = crypt_init_by_name (&cd, luks_device);
1959 if (ret != 0) {
1960 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1961 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
1962 crypt_free (cd);
1963 bd_utils_report_finished (progress_id, l_error->message);
1964 g_propagate_error (error, l_error);
1965 return FALSE;
1966 }
1967
1968 ret = crypt_suspend (cd, luks_device);
1969 if (ret != 0) {
1970 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
1971 "Failed to suspend device: %s", strerror_l (-ret, c_locale));
1972 crypt_free (cd);
1973 bd_utils_report_finished (progress_id, l_error->message);
1974 g_propagate_error (error, l_error);
1975 return FALSE;
1976 }
1977
1978 crypt_free (cd);
1979 bd_utils_report_finished (progress_id, "Completed");
1980 return TRUE;
1981 }
1982
1983 /**
1984 * bd_crypto_luks_resume:
1985 * @luks_device: LUKS device to resume
1986 * @context: key slot context (passphrase/keyfile/token...) for @luks_device
1987 * @error: (out) (optional): place to store error (if any)
1988 *
1989 * Supported @context types for this function: passphrase, key file
1990 *
1991 * Returns: whether the given @luks_device was successfully resumed or not
1992 *
1993 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_SUSPEND_RESUME
1994 */
1995 gboolean bd_crypto_luks_resume (const gchar *luks_device, BDCryptoKeyslotContext *context, GError **error) {
1996 struct crypt_device *cd = NULL;
1997 gchar *key_buffer = NULL;
1998 gsize buf_len = 0;
1999 gint ret = 0;
2000 guint64 progress_id = 0;
2001 gchar *msg = NULL;
2002 GError *l_error = NULL;
2003
2004 msg = g_strdup_printf ("Started resuming '%s' LUKS device", luks_device);
2005 progress_id = bd_utils_report_started (msg);
2006 g_free (msg);
2007
2008 ret = crypt_init_by_name (&cd, luks_device);
2009 if (ret != 0) {
2010 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2011 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2012 crypt_free (cd);
2013 bd_utils_report_finished (progress_id, l_error->message);
2014 g_propagate_error (error, l_error);
2015 return FALSE;
2016 }
2017
2018 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2019 if (ret != 0) {
2020 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2021 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
2022 crypt_free (cd);
2023 bd_utils_report_finished (progress_id, l_error->message);
2024 g_propagate_error (error, l_error);
2025 return FALSE;
2026 }
2027
2028 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
2029 ret = crypt_resume_by_passphrase (cd, luks_device, CRYPT_ANY_SLOT,
2030 (const char *) context->u.passphrase.pass_data,
2031 context->u.passphrase.data_len);
2032 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
2033 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buffer, &buf_len,
2034 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
2035 if (ret != 0) {
2036 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
2037 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile, strerror_l (-ret, c_locale));
2038 crypt_free (cd);
2039 bd_utils_report_finished (progress_id, l_error->message);
2040 g_propagate_error (error, l_error);
2041 return FALSE;
2042 }
2043 ret = crypt_resume_by_passphrase (cd, luks_device, CRYPT_ANY_SLOT, key_buffer, buf_len);
2044 crypt_safe_free (key_buffer);
2045 } else {
2046 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
2047 "Only 'passphrase' and 'key file' context types are valid for LUKS resume.");
2048 bd_utils_report_finished (progress_id, l_error->message);
2049 g_propagate_error (error, l_error);
2050 crypt_free (cd);
2051 return FALSE;
2052 }
2053
2054 if (ret < 0) {
2055 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2056 "Failed to resume device: %s", strerror_l (-ret, c_locale));
2057 crypt_free (cd);
2058 bd_utils_report_finished (progress_id, l_error->message);
2059 g_propagate_error (error, l_error);
2060 return FALSE;
2061 }
2062
2063 crypt_free (cd);
2064 bd_utils_report_finished (progress_id, "Completed");
2065 return TRUE;
2066 }
2067
2068 /**
2069 * bd_crypto_luks_kill_slot:
2070 * @device: device to kill slot on
2071 * @slot: keyslot to destroy
2072 * @error: (out) (optional): place to store error (if any)
2073 *
2074 * Note: This can destroy last remaining keyslot without confirmation making
2075 * the LUKS device permanently inaccessible.
2076 *
2077 * Returns: whether the given @slot was successfully destroyed or not
2078 *
2079 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_REMOVE_KEY
2080 */
2081 gboolean bd_crypto_luks_kill_slot (const gchar *device, gint slot, GError **error) {
2082 struct crypt_device *cd = NULL;
2083 gint ret = 0;
2084 guint64 progress_id = 0;
2085 gchar *msg = NULL;
2086 GError *l_error = NULL;
2087
2088 msg = g_strdup_printf ("Started killing slot %d on LUKS device '%s'", slot, device);
2089 progress_id = bd_utils_report_started (msg);
2090 g_free (msg);
2091
2092 ret = crypt_init (&cd, device);
2093 if (ret != 0) {
2094 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2095 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2096 bd_utils_report_finished (progress_id, l_error->message);
2097 g_propagate_error (error, l_error);
2098 return FALSE;
2099 }
2100
2101 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2102 if (ret != 0) {
2103 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2104 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
2105 crypt_free (cd);
2106 bd_utils_report_finished (progress_id, l_error->message);
2107 g_propagate_error (error, l_error);
2108 return FALSE;
2109 }
2110
2111 ret = crypt_keyslot_destroy (cd, slot);
2112 if (ret != 0) {
2113 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2114 "Failed to destroy keyslot: %s", strerror_l (-ret, c_locale));
2115 crypt_free (cd);
2116 bd_utils_report_finished (progress_id, l_error->message);
2117 g_propagate_error (error, l_error);
2118 return FALSE;
2119 }
2120
2121 crypt_free (cd);
2122 bd_utils_report_finished (progress_id, "Completed");
2123 return TRUE;
2124 }
2125
2126 /**
2127 * bd_crypto_luks_header_backup:
2128 * @device: device to backup the LUKS header
2129 * @backup_file: file to save the header backup to
2130 * @error: (out) (optional): place to store error (if any)
2131 *
2132 * Returns: whether the given backup of @device was successfully written to
2133 * @backup_file or not
2134 *
2135 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_BACKUP_RESTORE
2136 */
2137 gboolean bd_crypto_luks_header_backup (const gchar *device, const gchar *backup_file, GError **error) {
2138 struct crypt_device *cd = NULL;
2139 gint ret = 0;
2140 guint64 progress_id = 0;
2141 gchar *msg = NULL;
2142 GError *l_error = NULL;
2143
2144 msg = g_strdup_printf ("Started header backup of LUKS device '%s'", device);
2145 progress_id = bd_utils_report_started (msg);
2146 g_free (msg);
2147
2148 ret = crypt_init (&cd, device);
2149 if (ret != 0) {
2150 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2151 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2152 bd_utils_report_finished (progress_id, l_error->message);
2153 g_propagate_error (error, l_error);
2154 return FALSE;
2155 }
2156
2157 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2158 if (ret != 0) {
2159 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2160 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
2161 crypt_free (cd);
2162 bd_utils_report_finished (progress_id, l_error->message);
2163 g_propagate_error (error, l_error);
2164 return FALSE;
2165 }
2166
2167 ret = crypt_header_backup (cd, NULL, backup_file);
2168 if (ret != 0) {
2169 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2170 "Failed to backup LUKS header: %s", strerror_l (-ret, c_locale));
2171 crypt_free (cd);
2172 bd_utils_report_finished (progress_id, l_error->message);
2173 g_propagate_error (error, l_error);
2174 return FALSE;
2175 }
2176
2177 crypt_free (cd);
2178 bd_utils_report_finished (progress_id, "Completed");
2179 return TRUE;
2180 }
2181
2182 /**
2183 * bd_crypto_luks_header_restore:
2184 * @device: device to restore the LUKS header to
2185 * @backup_file: existing file with a LUKS header backup
2186 * @error: (out) (optional): place to store error (if any)
2187 *
2188 * Returns: whether the given @device LUKS header was successfully restored
2189 * from @backup_file
2190 *
2191 *
2192 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_BACKUP_RESTORE
2193 */
2194 gboolean bd_crypto_luks_header_restore (const gchar *device, const gchar *backup_file, GError **error) {
2195 struct crypt_device *cd = NULL;
2196 gint ret = 0;
2197 guint64 progress_id = 0;
2198 gchar *msg = NULL;
2199 GError *l_error = NULL;
2200
2201 msg = g_strdup_printf ("Started LUKS header restore on device '%s'", device);
2202 progress_id = bd_utils_report_started (msg);
2203 g_free (msg);
2204
2205 ret = crypt_init (&cd, device);
2206 if (ret != 0) {
2207 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2208 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2209 bd_utils_report_finished (progress_id, l_error->message);
2210 g_propagate_error (error, l_error);
2211 return FALSE;
2212 }
2213
2214 ret = crypt_header_restore (cd, NULL, backup_file);
2215 if (ret != 0) {
2216 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2217 "Failed to restore LUKS header: %s", strerror_l (-ret, c_locale));
2218 crypt_free (cd);
2219 bd_utils_report_finished (progress_id, l_error->message);
2220 g_propagate_error (error, l_error);
2221 return FALSE;
2222 }
2223
2224 crypt_free (cd);
2225 bd_utils_report_finished (progress_id, "Completed");
2226 return TRUE;
2227 }
2228
2229 /**
2230 * bd_crypto_luks_check_label:
2231 * @label: (nullable): label to check
2232 * @subsystem: (nullable): subsystem to check
2233 * @error: (out) (optional): place to store error
2234 *
2235 * Returns: whether @label and @subsystem are valid for LUKS2 or not
2236 * (reason is provided in @error)
2237 *
2238 * Tech category: always available
2239 */
2240 gboolean bd_crypto_luks_check_label (const gchar *label, const gchar *subsystem, GError **error) {
2241 if (label && strlen (label) > 47) {
2242 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_PARAMS,
2243 "Label for LUKS must be at most 47 characters long");
2244 return FALSE;
2245 }
2246
2247 if (subsystem && strlen (subsystem) > 47) {
2248 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_PARAMS,
2249 "Subsystem for LUKS must be at most 47 characters long");
2250 return FALSE;
2251 }
2252
2253 return TRUE;
2254 }
2255
2256 /**
2257 * bd_crypto_luks_set_label:
2258 * @device: device to set label on
2259 * @label: (nullable): label to set
2260 * @subsystem: (nullable): subsystem to set
2261 * @error: (out) (optional): place to store error (if any)
2262 *
2263 * Returns: whether the given @label and @subsystem were successfully set or not
2264 *
2265 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_MODIFY
2266 */
2267 gboolean bd_crypto_luks_set_label (const gchar *device, const gchar *label, const gchar *subsystem, GError **error) {
2268 struct crypt_device *cd = NULL;
2269 gint ret = 0;
2270
2271 ret = crypt_init (&cd, device);
2272 if (ret != 0) {
2273 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2274 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2275 return FALSE;
2276 }
2277
2278 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2279 if (ret != 0) {
2280 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2281 "Failed to load device: %s", strerror_l (-ret, c_locale));
2282 crypt_free (cd);
2283 return FALSE;
2284 }
2285
2286 if (g_strcmp0 (crypt_get_type (cd), CRYPT_LUKS2) != 0) {
2287 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
2288 "Label can be set only on LUKS 2 devices.");
2289 crypt_free (cd);
2290 return FALSE;
2291 }
2292
2293 ret = crypt_set_label (cd, label, subsystem);
2294 if (ret != 0) {
2295 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2296 "Failed to set label: %s", strerror_l (-ret, c_locale));
2297 crypt_free (cd);
2298 return FALSE;
2299 }
2300
2301 crypt_free (cd);
2302
2303 return TRUE;
2304 }
2305
2306 /**
2307 * bd_crypto_luks_set_uuid:
2308 * @device: device to set UUID on
2309 * @uuid: (nullable): UUID to set or %NULL to generate a new one
2310 * @error: (out) (optional): place to store error (if any)
2311 *
2312 * Returns: whether the given @uuid was successfully set or not
2313 *
2314 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_MODIFY
2315 */
2316 gboolean bd_crypto_luks_set_uuid (const gchar *device, const gchar *uuid, GError **error) {
2317 struct crypt_device *cd = NULL;
2318 gint ret = 0;
2319
2320 ret = crypt_init (&cd, device);
2321 if (ret != 0) {
2322 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2323 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2324 return FALSE;
2325 }
2326
2327 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2328 if (ret != 0) {
2329 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2330 "Failed to load device: %s", strerror_l (-ret, c_locale));
2331 crypt_free (cd);
2332 return FALSE;
2333 }
2334
2335 ret = crypt_set_uuid (cd, uuid);
2336 if (ret != 0) {
2337 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2338 "Failed to set UUID: %s", strerror_l (-ret, c_locale));
2339 crypt_free (cd);
2340 return FALSE;
2341 }
2342
2343 crypt_free (cd);
2344
2345 return TRUE;
2346 }
2347
2348 /**
2349 * bd_crypto_luks_convert:
2350 * @device: a LUKS device to convert to a different version of LUKS
2351 * @target_version: the LUKS version to convert to
2352 * @error: (out) (optional): place to store error (if any)
2353 *
2354 * Returns: whether the @device was converted to @target_version.
2355 * False, if the @device is already in the @target_version format.
2356 *
2357 * Warning: LUKS header loss is possible. See bd_crypto_luks_header_backup() and bd_crypto_luks_header_restore()
2358 *
2359 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_MODIFY
2360 */
2361 gboolean bd_crypto_luks_convert (const gchar *device, BDCryptoLUKSVersion target_version, GError **error) {
2362 struct crypt_device *cd = NULL;
2363 const char *cd_type;
2364 const char *target_type;
2365 gint ret = 0;
2366
2367 ret = crypt_init (&cd, device);
2368 if (ret != 0) {
2369 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2370 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2371 return FALSE;
2372 }
2373
2374 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2375 if (ret != 0) {
2376 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2377 "Failed to load device: %s", strerror_l (-ret, c_locale));
2378 crypt_free (cd);
2379 return FALSE;
2380 }
2381
2382 cd_type = crypt_get_type (cd);
2383 if (g_strcmp0 (cd_type, CRYPT_LUKS1) != 0 &&
2384 g_strcmp0 (cd_type, CRYPT_LUKS2) != 0) {
2385 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_CONVERT_FAILED,
2386 "It is possible to convert only between LUKS1 and LUKS2 formats. Device %s is of type: %s", device, cd_type);
2387 crypt_free (cd);
2388 return FALSE;
2389 }
2390
2391 target_type = target_version == BD_CRYPTO_LUKS_VERSION_LUKS1 ? CRYPT_LUKS1 : CRYPT_LUKS2;
2392 if (g_strcmp0 (cd_type, target_type) == 0) {
2393 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_CONVERT_FAILED,
2394 "Conversion to the %s type was requested, but device %s is already of type: %s", target_type, device, cd_type);
2395 crypt_free (cd);
2396 return FALSE;
2397 }
2398
2399 ret = crypt_convert (cd, target_type, NULL);
2400 if (ret != 0) {
2401 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2402 "Conversion failed: %s", strerror_l (-ret, c_locale));
2403 crypt_free (cd);
2404 return FALSE;
2405 }
2406
2407 crypt_free (cd);
2408 return TRUE;
2409 }
2410
2411 /**
2412 * bd_crypto_luks_set_persistent_flags:
2413 * @device: a LUKS device to set the persistent flags on
2414 * @flags: flags to set
2415 * @error: (out) (optional): place to store error (if any)
2416 *
2417 * Note: This function is valid only for LUKS2.
2418 *
2419 * Returns: whether the given @flags were successfully set or not
2420 *
2421 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_MODIFY
2422 */
2423 gboolean bd_crypto_luks_set_persistent_flags (const gchar *device, BDCryptoLUKSPersistentFlags flags, GError **error) {
2424 struct crypt_device *cd = NULL;
2425 gint ret = 0;
2426 guint32 crypt_flags = 0;
2427
2428 ret = crypt_init (&cd, device);
2429 if (ret != 0) {
2430 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2431 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2432 return FALSE;
2433 }
2434
2435 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2436 if (ret != 0) {
2437 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2438 "Failed to load device: %s", strerror_l (-ret, c_locale));
2439 crypt_free (cd);
2440 return FALSE;
2441 }
2442
2443 if (g_strcmp0 (crypt_get_type (cd), CRYPT_LUKS2) != 0) {
2444 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2445 "Persistent flags can be set only on LUKS v2");
2446 crypt_free (cd);
2447 return FALSE;
2448 }
2449
2450 if (flags & BD_CRYPTO_LUKS_ACTIVATE_ALLOW_DISCARDS)
2451 crypt_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
2452 if (flags & BD_CRYPTO_LUKS_ACTIVATE_SAME_CPU_CRYPT)
2453 crypt_flags |= CRYPT_ACTIVATE_SAME_CPU_CRYPT;
2454 if (flags & BD_CRYPTO_LUKS_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS)
2455 crypt_flags |= CRYPT_ACTIVATE_SUBMIT_FROM_CRYPT_CPUS;
2456 if (flags & BD_CRYPTO_LUKS_ACTIVATE_NO_JOURNAL)
2457 crypt_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
2458 if (flags & BD_CRYPTO_LUKS_ACTIVATE_NO_READ_WORKQUEUE)
2459 crypt_flags |= CRYPT_ACTIVATE_NO_READ_WORKQUEUE;
2460 if (flags & BD_CRYPTO_LUKS_ACTIVATE_NO_WRITE_WORKQUEUE)
2461 crypt_flags |= CRYPT_ACTIVATE_NO_WRITE_WORKQUEUE;
2462 if (flags & BD_CRYPTO_LUKS_ACTIVATE_HIGH_PRIORITY) {
2463 #ifdef LIBCRYPTSETUP_28
2464 crypt_flags |= CRYPT_ACTIVATE_HIGH_PRIORITY;
2465 #else
2466 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
2467 "Libcryptsetup 2.8 or newer is needed for 'high priority' flag support");
2468 crypt_free (cd);
2469 return FALSE;
2470 #endif
2471 }
2472
2473
2474 ret = crypt_persistent_flags_set (cd, CRYPT_FLAGS_ACTIVATION, crypt_flags);
2475 if (ret != 0) {
2476 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2477 "Failed to set flags: %s", strerror_l (-ret, c_locale));
2478 crypt_free (cd);
2479 return FALSE;
2480 }
2481
2482 crypt_free (cd);
2483
2484 return TRUE;
2485 }
2486
2487 static gint synced_close (gint fd) {
2488 gint ret = 0;
2489 ret = fsync (fd);
2490 if (close (fd) != 0)
2491 ret = 1;
2492 return ret;
2493 }
2494
2495 static gboolean get_subsystem_label (const gchar *device, gchar **subsystem, gchar **label, GError **error) {
2496 blkid_probe probe = NULL;
2497 gint fd = 0;
2498 gint status = 0;
2499 const gchar *value = NULL;
2500
2501 probe = blkid_new_probe ();
2502 if (!probe) {
2503 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2504 "Failed to create a probe for the device '%s'", device);
2505 return FALSE;
2506 }
2507
2508 fd = open (device, O_RDONLY|O_CLOEXEC);
2509 if (fd == -1) {
2510 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2511 "Failed to create a probe for the device '%s'", device);
2512 blkid_free_probe (probe);
2513 return FALSE;
2514 }
2515
2516 status = blkid_probe_set_device (probe, fd, 0, 0);
2517 if (status != 0) {
2518 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2519 "Failed to create a probe for the device '%s'", device);
2520 blkid_free_probe (probe);
2521 synced_close (fd);
2522 return FALSE;
2523 }
2524
2525 blkid_probe_enable_partitions (probe, 1);
2526
2527 status = blkid_do_probe (probe);
2528 if (status != 0) {
2529 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2530 "Failed to probe the device '%s'", device);
2531 blkid_free_probe (probe);
2532 synced_close (fd);
2533 return FALSE;
2534 }
2535
2536 status = blkid_probe_has_value (probe, "LABEL");
2537
2538 if (status == 0)
2539 *label = g_strdup ("");
2540 else {
2541 status = blkid_probe_lookup_value (probe, "LABEL", &value, NULL);
2542 if (status != 0) {
2543 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2544 "Failed to get label for the device '%s'", device);
2545 blkid_free_probe (probe);
2546 synced_close (fd);
2547 return FALSE;
2548 }
2549
2550 if (value)
2551 *label = g_strdup (value);
2552 else
2553 *label = g_strdup ("");
2554 }
2555
2556 status = blkid_probe_has_value (probe, "SUBSYSTEM");
2557 if (status == 0)
2558 *subsystem = g_strdup ("");
2559 else {
2560 status = blkid_probe_lookup_value (probe, "SUBSYSTEM", &value, NULL);
2561 if (status != 0) {
2562 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2563 "Failed to get subsystem for the device '%s'", device);
2564 g_free (*label);
2565 *label = NULL;
2566 blkid_free_probe (probe);
2567 synced_close (fd);
2568 return FALSE;
2569 }
2570
2571 if (value)
2572 *subsystem = g_strdup (value);
2573 else
2574 *subsystem = g_strdup ("");
2575 }
2576
2577 blkid_free_probe (probe);
2578 synced_close (fd);
2579
2580 return TRUE;
2581 }
2582
2583 /**
2584 * bd_crypto_luks_info:
2585 * @device: a device to get information about
2586 * @error: (out) (optional): place to store error (if any)
2587 *
2588 * Returns (transfer full): information about the @device or %NULL in case of error
2589 *
2590 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
2591 */
2592 BDCryptoLUKSInfo* bd_crypto_luks_info (const gchar *device, GError **error) {
2593 struct crypt_device *cd = NULL;
2594 BDCryptoLUKSInfo *info = NULL;
2595 const gchar *version = NULL;
2596 gint ret;
2597 gboolean success = FALSE;
2598
2599 ret = crypt_init (&cd, device);
2600 if (ret != 0) {
2601 /* not a block device, try init_by_name */
2602 crypt_free (cd);
2603 cd = NULL;
2604 ret = crypt_init_by_name (&cd, device);
2605 } else {
2606 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2607 if (ret != 0) {
2608 /* not a LUKS device, try init_by_name */
2609 crypt_free (cd);
2610 cd = NULL;
2611 ret = crypt_init_by_name (&cd, device);
2612 }
2613 }
2614
2615 if (ret != 0) {
2616 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2617 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2618 crypt_free (cd);
2619 return NULL;
2620 }
2621
2622 info = g_new0 (BDCryptoLUKSInfo, 1);
2623
2624 version = crypt_get_type (cd);
2625 if (g_strcmp0 (version, CRYPT_LUKS1) == 0)
2626 info->version = BD_CRYPTO_LUKS_VERSION_LUKS1;
2627 else if (g_strcmp0 (version, CRYPT_LUKS2) == 0)
2628 info->version = BD_CRYPTO_LUKS_VERSION_LUKS2;
2629 else {
2630 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
2631 "Unknown or unsupported LUKS version");
2632 bd_crypto_luks_info_free (info);
2633 crypt_free (cd);
2634 return NULL;
2635 }
2636
2637 info->cipher = g_strdup (crypt_get_cipher (cd));
2638 info->mode = g_strdup (crypt_get_cipher_mode (cd));
2639 info->uuid = g_strdup (crypt_get_uuid (cd));
2640 info->backing_device = g_strdup (crypt_get_device_name (cd));
2641 ret = crypt_get_sector_size (cd);
2642 info->sector_size = ret > 0 ? ret : 0;
2643 info->metadata_size = SECTOR_SIZE * crypt_get_data_offset (cd);
2644
2645 if (info->version == BD_CRYPTO_LUKS_VERSION_LUKS2) {
2646 success = get_subsystem_label (crypt_get_device_name (cd) , &(info->subsystem), &(info->label), error);
2647 if (!success) {
2648 crypt_free (cd);
2649 bd_crypto_luks_info_free (info);
2650 return NULL;
2651 }
2652 } else {
2653 info->label = g_strdup ("");
2654 info->subsystem = g_strdup ("");
2655 }
2656
2657 #ifdef LIBCRYPTSETUP_27
2658 ret = crypt_get_hw_encryption_type (cd);
2659 if (ret < 0) {
2660 info->hw_encryption = BD_CRYPTO_LUKS_HW_ENCRYPTION_UNKNOWN;
2661 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to get HW encryption type: %s", strerror_l (-ret, c_locale));
2662 } else {
2663 switch (ret) {
2664 case CRYPT_SW_ONLY:
2665 info->hw_encryption = BD_CRYPTO_LUKS_HW_ENCRYPTION_SW_ONLY;
2666 break;
2667 case CRYPT_SW_AND_OPAL_HW:
2668 info->hw_encryption = BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_AND_SW;
2669 break;
2670 case CRYPT_OPAL_HW_ONLY:
2671 info->hw_encryption = BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_ONLY;
2672 break;
2673 default:
2674 bd_utils_log_format (BD_UTILS_LOG_WARNING, "Unknown HW encryption type: %d", ret);
2675 info->hw_encryption = BD_CRYPTO_LUKS_HW_ENCRYPTION_UNKNOWN;
2676 break;
2677 }
2678 }
2679 #else
2680 info->hw_encryption = BD_CRYPTO_LUKS_HW_ENCRYPTION_UNKNOWN;
2681 #endif
2682
2683 crypt_free (cd);
2684
2685 return info;
2686 }
2687
2688 /**
2689 * bd_crypto_bitlk_info:
2690 * @device: a device to get information about
2691 * @error: (out) (optional): place to store error (if any)
2692 *
2693 * Returns (transfer full): information about the @device or %NULL in case of error
2694 *
2695 * Tech category: %BD_CRYPTO_TECH_BITLK-%BD_CRYPTO_TECH_MODE_QUERY
2696 */
2697 BDCryptoBITLKInfo* bd_crypto_bitlk_info (const gchar *device, GError **error) {
2698 struct crypt_device *cd = NULL;
2699 BDCryptoBITLKInfo *info = NULL;
2700 gint ret;
2701
2702 ret = crypt_init (&cd, device);
2703 if (ret != 0) {
2704 /* not a block device, try init_by_name */
2705 crypt_free (cd);
2706 cd = NULL;
2707 ret = crypt_init_by_name (&cd, device);
2708 } else {
2709 ret = crypt_load (cd, CRYPT_BITLK, NULL);
2710 if (ret != 0) {
2711 /* not a BITLK device, try init_by_name */
2712 crypt_free (cd);
2713 cd = NULL;
2714 ret = crypt_init_by_name (&cd, device);
2715 }
2716 }
2717
2718 if (ret != 0) {
2719 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2720 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2721 crypt_free (cd);
2722 return NULL;
2723 }
2724
2725 info = g_new0 (BDCryptoBITLKInfo, 1);
2726
2727 info->cipher = g_strdup (crypt_get_cipher (cd));
2728 info->mode = g_strdup (crypt_get_cipher_mode (cd));
2729 info->uuid = g_strdup (crypt_get_uuid (cd));
2730 info->backing_device = g_strdup (crypt_get_device_name (cd));
2731 ret = crypt_get_sector_size (cd);
2732 info->sector_size = ret > 0 ? ret : 0;
2733
2734 crypt_free (cd);
2735
2736 return info;
2737 }
2738
2739 /**
2740 * bd_crypto_integrity_info:
2741 * @device: a device to get information about
2742 * @error: (out) (optional): place to store error (if any)
2743 *
2744 * Returns (transfer full): information about the @device or %NULL in case of error
2745 *
2746 * Tech category: %BD_CRYPTO_TECH_INTEGRITY%BD_CRYPTO_TECH_MODE_QUERY
2747 */
2748 BDCryptoIntegrityInfo* bd_crypto_integrity_info (const gchar *device, GError **error) {
2749 struct crypt_device *cd = NULL;
2750 struct crypt_params_integrity ip = ZERO_INIT;
2751 BDCryptoIntegrityInfo *info = NULL;
2752 gint ret;
2753
2754 ret = crypt_init (&cd, device);
2755 if (ret != 0) {
2756 /* not a block device, try init_by_name */
2757 crypt_free (cd);
2758 cd = NULL;
2759 ret = crypt_init_by_name (&cd, device);
2760 } else {
2761 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2762 if (ret != 0) {
2763 /* not a LUKS device, try integrity */
2764 ret = crypt_load (cd, CRYPT_INTEGRITY, NULL);
2765 if (ret != 0) {
2766 crypt_free (cd);
2767 cd = NULL;
2768 ret = crypt_init_by_name (&cd, device);
2769 }
2770 }
2771 }
2772
2773 if (ret != 0) {
2774 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2775 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2776 crypt_free (cd);
2777 return NULL;
2778 }
2779
2780 ret = crypt_get_integrity_info (cd, &ip);
2781 if (ret != 0) {
2782 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2783 "Failed to get information about device: %s", strerror_l (-ret, c_locale));
2784 crypt_free (cd);
2785 return NULL;
2786 }
2787
2788 info = g_new0 (BDCryptoIntegrityInfo, 1);
2789
2790 info->algorithm = g_strdup (ip.integrity);
2791 info->key_size = ip.integrity_key_size;
2792 info->sector_size = ip.sector_size;
2793 info->tag_size = ip.tag_size;
2794 info->interleave_sectors = ip.interleave_sectors;
2795 info->journal_size = ip.journal_size;
2796 info->journal_crypt = g_strdup (ip.journal_crypt);
2797 info->journal_integrity = g_strdup (ip.journal_integrity);
2798
2799 crypt_free (cd);
2800 return info;
2801 }
2802
2803 /* added in cryptsetup 2.4.0 */
2804 #ifndef LIBCRYPTSETUP_24
2805 static int crypt_token_max (const char *type G_GNUC_UNUSED) {
2806 return 32;
2807 }
2808 #endif
2809
2810
2811 /**
2812 * bd_crypto_luks_token_info:
2813 * @device: a device to get LUKS2 token information about
2814 * @error: (out) (optional): place to store error (if any)
2815 *
2816 * Returns: (array zero-terminated=1) (transfer full): information about tokens on @device
2817 *
2818 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_QUERY
2819 */
2820 BDCryptoLUKSTokenInfo** bd_crypto_luks_token_info (const gchar *device, GError **error) {
2821 struct crypt_device *cd = NULL;
2822 GPtrArray *tokens = NULL;
2823 BDCryptoLUKSTokenInfo *info = NULL;
2824 crypt_token_info token_info;
2825 const gchar *type = NULL;
2826 gint ret;
2827 gint token_it, keyslot_it;
2828
2829 ret = crypt_init (&cd, device);
2830 if (ret != 0) {
2831 /* not a block device, try init_by_name */
2832 crypt_free (cd);
2833 cd = NULL;
2834 ret = crypt_init_by_name (&cd, device);
2835 } else {
2836 ret = crypt_load (cd, CRYPT_LUKS, NULL);
2837 if (ret != 0) {
2838 /* not a LUKS device, try init_by_name */
2839 crypt_free (cd);
2840 cd = NULL;
2841 ret = crypt_init_by_name (&cd, device);
2842 }
2843 }
2844
2845 if (ret != 0) {
2846 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2847 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2848 crypt_free (cd);
2849 return NULL;
2850 }
2851
2852 if (g_strcmp0 (crypt_get_type (cd), CRYPT_LUKS2) != 0) {
2853 crypt_free (cd);
2854 return NULL;
2855 }
2856
2857 tokens = g_ptr_array_new ();
2858
2859 for (token_it = 0; token_it < crypt_token_max (CRYPT_LUKS2); token_it++) {
2860 token_info = crypt_token_status (cd, token_it, &type);
2861 if (token_info == CRYPT_TOKEN_INVALID || token_info == CRYPT_TOKEN_INACTIVE)
2862 continue;
2863
2864 info = g_new0 (BDCryptoLUKSTokenInfo, 1);
2865 info->id = token_it;
2866 info->type = g_strdup (type);
2867 info->keyslot = -1;
2868
2869 for (keyslot_it = 0; keyslot_it < crypt_keyslot_max (CRYPT_LUKS2); keyslot_it++) {
2870 ret = crypt_token_is_assigned (cd, token_it, keyslot_it);
2871 if (ret == 0) {
2872 info->keyslot = keyslot_it;
2873 break;
2874 }
2875 }
2876
2877 g_ptr_array_add (tokens, info);
2878 }
2879
2880 crypt_free (cd);
2881
2882 /* returning NULL-terminated array of BDCryptoLUKSTokenInfo */
2883 g_ptr_array_add (tokens, NULL);
2884 return (BDCryptoLUKSTokenInfo **) g_ptr_array_free (tokens, FALSE);
2885 }
2886
2887 static int _wipe_progress (guint64 size, guint64 offset, void *usrptr) {
2888 /* "convert" the progress from 0-100 to 50-100 because wipe starts at 50 in bd_crypto_integrity_format */
2889 gdouble progress = 50 + (((gdouble) offset / size) * 100) / 2;
2890 bd_utils_report_progress (*(guint64 *) usrptr, progress, "Integrity device wipe in progress");
2891
2892 return 0;
2893 }
2894
2895 /**
2896 * bd_crypto_integrity_format:
2897 * @device: a device to format as integrity
2898 * @algorithm: integrity algorithm specification (e.g. "crc32c" or "sha256")
2899 * @wipe: whether to wipe the device after format; a device that is not initially wiped will contain invalid checksums
2900 * @context: (nullable): key slot context (passphrase/keyfile/token...) for this device
2901 * @extra: (nullable): extra arguments for integrity format creation
2902 * @error: (out) (optional): place to store error (if any)
2903 *
2904 * Formats the given @device as integrity according to the other parameters given.
2905 *
2906 * Supported @context types for this function: volume key
2907 *
2908 * Returns: whether the given @device was successfully formatted as integrity or not
2909 * (the @error) contains the error in such cases)
2910 *
2911 * Tech category: %BD_CRYPTO_TECH_INTEGRITY-%BD_CRYPTO_TECH_MODE_CREATE
2912 */
2913 gboolean bd_crypto_integrity_format (const gchar *device, const gchar *algorithm, gboolean wipe, BDCryptoKeyslotContext *context, BDCryptoIntegrityExtra *extra, GError **error) {
2914 struct crypt_device *cd = NULL;
2915 gint ret;
2916 guint64 progress_id = 0;
2917 gchar *msg = NULL;
2918 struct crypt_params_integrity params = ZERO_INIT;
2919 g_autofree gchar *tmp_name = NULL;
2920 g_autofree gchar *tmp_path = NULL;
2921 g_autofree gchar *dev_name = NULL;
2922 GError *l_error = NULL;
2923
2924 msg = g_strdup_printf ("Started formatting '%s' as integrity device", device);
2925 progress_id = bd_utils_report_started (msg);
2926 g_free (msg);
2927
2928 if (context && context->type != BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_VOLUME_KEY) {
2929 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
2930 "Only 'volume key' context type is valid for integrity format.");
2931 bd_utils_report_finished (progress_id, l_error->message);
2932 g_propagate_error (error, l_error);
2933 return FALSE;
2934 }
2935
2936 ret = crypt_init (&cd, device);
2937 if (ret != 0) {
2938 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2939 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
2940 bd_utils_report_finished (progress_id, l_error->message);
2941 g_propagate_error (error, l_error);
2942 return FALSE;
2943 }
2944
2945 if (extra) {
2946 params.sector_size = extra->sector_size;
2947 params.journal_size = extra->journal_size;
2948 params.journal_watermark = extra->journal_watermark;
2949 params.journal_commit_time = extra->journal_commit_time;
2950 params.interleave_sectors = extra->interleave_sectors;
2951 params.tag_size = extra->tag_size;
2952 params.buffer_sectors = extra->buffer_sectors;
2953 }
2954
2955 params.integrity_key_size = context ? context->u.volume_key.volume_key_size : 0;
2956 params.integrity = algorithm;
2957 params.tag_size = params.tag_size ? params.tag_size : 0;
2958
2959 ret = crypt_format (cd, CRYPT_INTEGRITY, NULL, NULL, NULL, NULL, 0, &params);
2960 if (ret != 0) {
2961 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
2962 "Failed to format device: %s", strerror_l (-ret, c_locale));
2963 crypt_free (cd);
2964 bd_utils_report_finished (progress_id, l_error->message);
2965 g_propagate_error (error, l_error);
2966 return FALSE;
2967 }
2968
2969 if (wipe) {
2970 bd_utils_report_progress (progress_id, 50, "Format created");
2971
2972 dev_name = g_path_get_basename (device);
2973 tmp_name = g_strdup_printf ("bd-temp-integrity-%s-%d", dev_name, g_random_int ());
2974 tmp_path = g_strdup_printf ("%s/%s", crypt_get_dir (), tmp_name);
2975
2976 ret = crypt_activate_by_volume_key (cd, tmp_name,
2977 context ? (const char *) context->u.volume_key.volume_key : NULL,
2978 context ? context->u.volume_key.volume_key_size : 0,
2979 CRYPT_ACTIVATE_PRIVATE | CRYPT_ACTIVATE_NO_JOURNAL);
2980 if (ret != 0) {
2981 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2982 "Failed to activate the newly created integrity device for wiping: %s",
2983 strerror_l (-ret, c_locale));
2984 crypt_free (cd);
2985 bd_utils_report_finished (progress_id, l_error->message);
2986 g_propagate_error (error, l_error);
2987 return FALSE;
2988 }
2989
2990 bd_utils_report_progress (progress_id, 50, "Starting to wipe the newly created integrity device");
2991 ret = crypt_wipe (cd, tmp_path, CRYPT_WIPE_ZERO, 0, 0, 1048576,
2992 0, &_wipe_progress, &progress_id);
2993 bd_utils_report_progress (progress_id, 100, "Wipe finished");
2994 if (ret != 0) {
2995 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
2996 "Failed to wipe the newly created integrity device: %s",
2997 strerror_l (-ret, c_locale));
2998
2999 ret = crypt_deactivate (cd, tmp_name);
3000 if (ret != 0)
3001 bd_utils_log_format (BD_UTILS_LOG_ERR, "Failed to deactivate temporary device %s", tmp_name);
3002
3003 crypt_free (cd);
3004 bd_utils_report_finished (progress_id, l_error->message);
3005 g_propagate_error (error, l_error);
3006 return FALSE;
3007 }
3008
3009 ret = crypt_deactivate (cd, tmp_name);
3010 if (ret != 0)
3011 bd_utils_log_format (BD_UTILS_LOG_ERR, "Failed to deactivate temporary device %s", tmp_name);
3012 }
3013
3014 crypt_free (cd);
3015 bd_utils_report_finished (progress_id, "Completed");
3016
3017 return TRUE;
3018 }
3019
3020 /**
3021 * bd_crypto_integrity_open:
3022 * @device: integrity device to open
3023 * @name: name for the opened @device
3024 * @algorithm: integrity algorithm specification (e.g. "crc32c" or "sha256")
3025 * @context: (nullable): key slot context (passphrase/keyfile/token...) for this device
3026 * @flags: flags for the integrity device activation
3027 * @extra: (nullable): extra arguments for integrity open
3028 * @error: (out) (optional): place to store error (if any)
3029 *
3030 * Supported @context types for this function: volume key
3031 *
3032 * Returns: whether the @device was successfully opened or not
3033 *
3034 * Tech category: %BD_CRYPTO_TECH_INTEGRITY-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3035 */
3036 gboolean bd_crypto_integrity_open (const gchar *device, const gchar *name, const gchar *algorithm, BDCryptoKeyslotContext *context, BDCryptoIntegrityOpenFlags flags, BDCryptoIntegrityExtra *extra, GError **error) {
3037 struct crypt_device *cd = NULL;
3038 gint ret = 0;
3039 guint64 progress_id = 0;
3040 gchar *msg = NULL;
3041 struct crypt_params_integrity params = ZERO_INIT;
3042 guint32 activate_flags = 0;
3043 GError *l_error = NULL;
3044
3045 msg = g_strdup_printf ("Started opening '%s' integrity device", device);
3046 progress_id = bd_utils_report_started (msg);
3047 g_free (msg);
3048
3049 if (context && context->type != BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_VOLUME_KEY) {
3050 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
3051 "Only 'volume key' context type is valid for integrity open.");
3052 bd_utils_report_finished (progress_id, l_error->message);
3053 g_propagate_error (error, l_error);
3054 return FALSE;
3055 }
3056
3057 params.integrity = algorithm;
3058 params.integrity_key_size = context ? context->u.volume_key.volume_key_size : 0;
3059
3060 if (extra) {
3061 params.sector_size = extra->sector_size;
3062 params.journal_size = extra->journal_size;
3063 params.journal_watermark = extra->journal_watermark;
3064 params.journal_commit_time = extra->journal_commit_time;
3065 params.interleave_sectors = extra->interleave_sectors;
3066 params.tag_size = extra->tag_size;
3067 params.buffer_sectors = extra->buffer_sectors;
3068 }
3069
3070
3071 if (flags & BD_CRYPTO_INTEGRITY_OPEN_NO_JOURNAL)
3072 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL;
3073 if (flags & BD_CRYPTO_INTEGRITY_OPEN_RECOVERY)
3074 activate_flags |= CRYPT_ACTIVATE_RECOVERY;
3075 if (flags & BD_CRYPTO_INTEGRITY_OPEN_RECALCULATE)
3076 activate_flags |= CRYPT_ACTIVATE_RECALCULATE;
3077 if (flags & BD_CRYPTO_INTEGRITY_OPEN_ALLOW_DISCARDS)
3078 activate_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
3079 if (flags & BD_CRYPTO_INTEGRITY_OPEN_NO_JOURNAL_BITMAP)
3080 activate_flags |= CRYPT_ACTIVATE_NO_JOURNAL_BITMAP;
3081
3082 if (flags & BD_CRYPTO_INTEGRITY_OPEN_RECALCULATE_RESET) {
3083 #ifndef CRYPT_ACTIVATE_RECALCULATE_RESET
3084 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_TECH_UNAVAIL,
3085 "Cannot reset integrity recalculation while activating %s, installed version of cryptsetup doesn't support this option.", device);
3086 bd_utils_report_finished (progress_id, l_error->message);
3087 g_propagate_error (error, l_error);
3088 return FALSE;
3089 #else
3090 activate_flags |= CRYPT_ACTIVATE_RECALCULATE_RESET;
3091 #endif
3092 }
3093
3094 if (!_is_dm_name_valid (name, &l_error)) {
3095 bd_utils_report_finished (progress_id, l_error->message);
3096 g_propagate_error (error, l_error);
3097 return FALSE;
3098 }
3099
3100 ret = crypt_init (&cd, device);
3101 if (ret != 0) {
3102 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3103 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
3104 bd_utils_report_finished (progress_id, l_error->message);
3105 g_propagate_error (error, l_error);
3106 return FALSE;
3107 }
3108
3109 ret = crypt_load (cd, CRYPT_INTEGRITY, &params);
3110 if (ret != 0) {
3111 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3112 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
3113 crypt_free (cd);
3114 bd_utils_report_finished (progress_id, l_error->message);
3115 g_propagate_error (error, l_error);
3116 return FALSE;
3117 }
3118
3119 ret = crypt_activate_by_volume_key (cd, name,
3120 context ? (const char *) context->u.volume_key.volume_key : NULL,
3121 context ? context->u.volume_key.volume_key_size : 0,
3122 activate_flags);
3123 if (ret < 0) {
3124 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3125 "Failed to activate device: %s", strerror_l (-ret, c_locale));
3126
3127 crypt_free (cd);
3128 bd_utils_report_finished (progress_id, l_error->message);
3129 g_propagate_error (error, l_error);
3130 return FALSE;
3131 }
3132
3133 crypt_free (cd);
3134 bd_utils_report_finished (progress_id, "Completed");
3135 return TRUE;
3136 }
3137
3138 /**
3139 * bd_crypto_integrity_close:
3140 * @integrity_device: integrity device to close
3141 * @error: (out) (optional): place to store error (if any)
3142 *
3143 * Returns: whether the given @integrity_device was successfully closed or not
3144 *
3145 * Tech category: %BD_CRYPTO_TECH_INTEGRITY-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3146 */
3147 gboolean bd_crypto_integrity_close (const gchar *integrity_device, GError **error) {
3148 return _crypto_close (integrity_device, "integrity", error);
3149 }
3150
3151 /**
3152 * bd_crypto_keyring_add_key:
3153 * @key_desc: kernel keyring key description
3154 * @key_data: (array length=data_len): a key to add to kernel keyring (may contain arbitrary binary data)
3155 * @data_len: length of the @key_data buffer
3156 * @error: (out) (optional): place to store error (if any)
3157 * *
3158 * Returns: whether the given key was successfully saved to kernel keyring or not
3159 *
3160 * Tech category: %BD_CRYPTO_TECH_KEYRING-%BD_CRYPTO_TECH_MODE_ADD_KEY
3161 */
3162 gboolean bd_crypto_keyring_add_key (const gchar *key_desc, const guint8 *key_data, gsize data_len, GError **error) {
3163 key_serial_t ret;
3164
3165 ret = add_key ("user", key_desc, key_data, data_len, KEY_SPEC_SESSION_KEYRING);
3166 if (ret < 0) {
3167 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYRING,
3168 "Failed to add key to kernel keyring: %s", strerror_l (errno, c_locale));
3169 return FALSE;
3170 }
3171
3172 return TRUE;
3173 }
3174
3175 /**
3176 * bd_crypto_device_seems_encrypted:
3177 * @device: the queried device
3178 * @error: (out) (optional): place to store error (if any)
3179 *
3180 * Determines whether a block device seems to be encrypted.
3181 *
3182 * TCRYPT volumes are not easily identifiable, because they have no
3183 * cleartext header, but are completely encrypted. This function is
3184 * used to determine whether a block device is a candidate for being
3185 * TCRYPT encrypted.
3186 *
3187 * To achieve this, we calculate the chi square value of the first
3188 * 512 Bytes and treat devices with a chi square value between 136
3189 * and 426 as candidates for being encrypted.
3190 * For the reasoning, see: https://gitlab.tails.boum.org/tails/blueprints/-/wikis/veracrypt/#detecting-veracrypt-volumes
3191 *
3192 * Returns: %TRUE if the given @device seems to be encrypted or %FALSE if not or
3193 * failed to determine (the @error) is populated with the error in such
3194 * cases)
3195 *
3196 * Tech category: %BD_CRYPTO_TECH_TRUECRYPT-%BD_CRYPTO_TECH_MODE_QUERY
3197 */
3198 gboolean bd_crypto_device_seems_encrypted (const gchar *device, GError **error) {
3199 gint fd = -1;
3200 guchar buf[SQUARE_BYTES_TO_CHECK];
3201 guint symbols[256] = {0};
3202 gfloat chi_square = 0.0;
3203 gfloat e = (gfloat) sizeof (buf) / (gfloat) 256.0;
3204 guint i;
3205 guint64 progress_id = 0;
3206 gchar *msg = NULL;
3207 GError *l_error = NULL;
3208
3209 msg = g_strdup_printf ("Started determining if device '%s' seems to be encrypted", device);
3210 progress_id = bd_utils_report_started (msg);
3211 g_free (msg);
3212
3213 fd = open (device, O_RDONLY);
3214 if (fd == -1) {
3215 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE, "Failed to open device");
3216 bd_utils_report_finished (progress_id, l_error->message);
3217 g_propagate_error (error, l_error);
3218 return FALSE;
3219 }
3220
3221 if (read (fd, buf, sizeof (buf)) != sizeof (buf)) {
3222 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE, "Failed to read device");
3223 bd_utils_report_finished (progress_id, l_error->message);
3224 g_propagate_error (error, l_error);
3225 close (fd);
3226 return FALSE;
3227 }
3228
3229 close (fd);
3230
3231 /* Calculate Chi Square */
3232 for (i = 0; i < sizeof (buf); i++)
3233 /* This is safe because the max value of buf[i] is < sizeof (symbols). */
3234 symbols[buf[i]]++;
3235 for (i = 0; i < 256; i++)
3236 chi_square += (symbols[i] - e) * (symbols[i] - e);
3237 chi_square /= e;
3238
3239 bd_utils_report_finished (progress_id, "Completed");
3240 return SQUARE_LOWER_LIMIT < chi_square && chi_square < SQUARE_UPPER_LIMIT;
3241 }
3242
3243 /**
3244 * bd_crypto_tc_open_flags:
3245 * @device: the device to open
3246 * @name: name for the TrueCrypt/VeraCrypt device
3247 * @context: (nullable): passphrase key slot context for this TrueCrypt/VeraCrypt volume
3248 * @flags: activation flags for the TrueCrypt/VeraCrypt device
3249 * @keyfiles: (nullable) (array zero-terminated=1): paths to the keyfiles for the TrueCrypt/VeraCrypt volume
3250 * @hidden: whether a hidden volume inside the volume should be opened
3251 * @system: whether to try opening as an encrypted system (with boot loader)
3252 * @veracrypt: whether to try VeraCrypt modes (TrueCrypt modes are tried anyway)
3253 * @veracrypt_pim: VeraCrypt PIM value (only used if @veracrypt is %TRUE)
3254 * @error: (out) (optional): place to store error (if any)
3255 *
3256 * Supported @context types for this function: passphrase
3257 *
3258 * Returns: whether the @device was successfully opened or not
3259 *
3260 * Tech category: %BD_CRYPTO_TECH_TRUECRYPT-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3261 */
3262 gboolean bd_crypto_tc_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, const gchar **keyfiles, gboolean hidden, gboolean system, gboolean veracrypt, guint32 veracrypt_pim, BDCryptoOpenFlags flags, GError **error) {
3263 struct crypt_device *cd = NULL;
3264 gint ret = 0;
3265 guint64 progress_id = 0;
3266 gchar *msg = NULL;
3267 struct crypt_params_tcrypt params = ZERO_INIT;
3268 gsize keyfiles_count = 0;
3269 guint i;
3270 GError *l_error = NULL;
3271 guint32 crypt_flags = 0;
3272
3273 if (!_is_dm_name_valid (name, error))
3274 return FALSE;
3275
3276 msg = g_strdup_printf ("Started opening '%s' TrueCrypt/VeraCrypt device", device);
3277 progress_id = bd_utils_report_started (msg);
3278 g_free (msg);
3279
3280 if (keyfiles) {
3281 for (i=0; *(keyfiles + i); i++);
3282 keyfiles_count = i;
3283 }
3284
3285 if (context && context->type != BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
3286 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
3287 "Only 'passphrase' context type is valid for TC open.");
3288 bd_utils_report_finished (progress_id, l_error->message);
3289 g_propagate_error (error, l_error);
3290 return FALSE;
3291 }
3292
3293 if ((context == NULL) && (keyfiles_count == 0)) {
3294 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_NO_KEY,
3295 "No passphrase nor key file specified, cannot open.");
3296 bd_utils_report_finished (progress_id, l_error->message);
3297 g_propagate_error (error, l_error);
3298 return FALSE;
3299 }
3300
3301 ret = crypt_init (&cd, device);
3302 if (ret != 0) {
3303 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3304 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
3305 bd_utils_report_finished (progress_id, l_error->message);
3306 g_propagate_error (error, l_error);
3307 return FALSE;
3308 }
3309
3310 params.passphrase = context ? (const char*) context->u.passphrase.pass_data : NULL;
3311 params.passphrase_size = context ? context->u.passphrase.data_len : 0;
3312 params.keyfiles = keyfiles;
3313 params.keyfiles_count = keyfiles_count;
3314
3315 if (veracrypt)
3316 params.flags |= CRYPT_TCRYPT_VERA_MODES;
3317 if (hidden)
3318 params.flags |= CRYPT_TCRYPT_HIDDEN_HEADER;
3319 if (system)
3320 params.flags |= CRYPT_TCRYPT_SYSTEM_HEADER;
3321 if (veracrypt && veracrypt_pim != 0)
3322 params.veracrypt_pim = veracrypt_pim;
3323
3324 ret = crypt_load (cd, CRYPT_TCRYPT, &params);
3325 if (ret != 0) {
3326 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3327 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
3328 crypt_free (cd);
3329 bd_utils_report_finished (progress_id, l_error->message);
3330 g_propagate_error (error, l_error);
3331 return FALSE;
3332 }
3333
3334 if (flags & BD_CRYPTO_OPEN_ALLOW_DISCARDS)
3335 crypt_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
3336 if (flags & BD_CRYPTO_OPEN_READONLY)
3337 crypt_flags |= CRYPT_ACTIVATE_READONLY;
3338
3339 ret = crypt_activate_by_volume_key (cd, name, NULL, 0, crypt_flags);
3340 if (ret < 0) {
3341 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3342 "Failed to activate device: %s", strerror_l (-ret, c_locale));
3343 crypt_free (cd);
3344 bd_utils_report_finished (progress_id, l_error->message);
3345 g_propagate_error (error, l_error);
3346 return FALSE;
3347 }
3348
3349 crypt_free (cd);
3350 bd_utils_report_finished (progress_id, "Completed");
3351 return TRUE;
3352 }
3353
3354 /**
3355 * bd_crypto_tc_open:
3356 * @device: the device to open
3357 * @name: name for the TrueCrypt/VeraCrypt device
3358 * @context: (nullable): passphrase key slot context for this TrueCrypt/VeraCrypt volume
3359 * @read_only: whether to open as read-only or not (meaning read-write)
3360 * @keyfiles: (nullable) (array zero-terminated=1): paths to the keyfiles for the TrueCrypt/VeraCrypt volume
3361 * @hidden: whether a hidden volume inside the volume should be opened
3362 * @system: whether to try opening as an encrypted system (with boot loader)
3363 * @veracrypt: whether to try VeraCrypt modes (TrueCrypt modes are tried anyway)
3364 * @veracrypt_pim: VeraCrypt PIM value (only used if @veracrypt is %TRUE)
3365 * @error: (out) (optional): place to store error (if any)
3366 *
3367 * Supported @context types for this function: passphrase
3368 *
3369 * Returns: whether the @device was successfully opened or not
3370 *
3371 * Tech category: %BD_CRYPTO_TECH_TRUECRYPT-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3372 */
3373 gboolean bd_crypto_tc_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, const gchar **keyfiles, gboolean hidden, gboolean system, gboolean veracrypt, guint32 veracrypt_pim, gboolean read_only, GError **error) {
3374 return bd_crypto_tc_open_flags (device, name, context, keyfiles, hidden, system, veracrypt, veracrypt_pim, read_only ? BD_CRYPTO_OPEN_READONLY : 0, error);
3375 }
3376
3377 /**
3378 * bd_crypto_tc_close:
3379 * @tc_device: TrueCrypt/VeraCrypt device to close
3380 * @error: (out) (optional): place to store error (if any)
3381 *
3382 * Returns: whether the given @tc_device was successfully closed or not
3383 *
3384 * Tech category: %BD_CRYPTO_TECH_TRUECRYPT-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3385 */
3386 gboolean bd_crypto_tc_close (const gchar *tc_device, GError **error) {
3387 return _crypto_close (tc_device, "TrueCrypt/VeraCrypt", error);
3388 }
3389
3390 #ifdef WITH_BD_ESCROW
3391 static gchar *always_fail_cb (gpointer data G_GNUC_UNUSED, const gchar *prompt G_GNUC_UNUSED, int echo G_GNUC_UNUSED) {
3392 return NULL;
3393 }
3394
3395 static gchar *give_passphrase_cb (gpointer data, const gchar *prompt G_GNUC_UNUSED, unsigned failed_attempts) {
3396 if (failed_attempts == 0)
3397 /* Return a copy of the passphrase that will be freed by volume_key */
3398 return g_strdup (data);
3399 return NULL;
3400 }
3401
3402 static void free_passphrase_cb (gpointer data) {
3403 g_free (data);
3404 }
3405
3406 /**
3407 * replace_char:
3408 *
3409 * Replaces all occurrences of @orig in @str with @new (in place).
3410 */
3411 static gchar *replace_char (gchar *str, gchar orig, gchar new) {
3412 gchar *pos = str;
3413 if (!str)
3414 return str;
3415
3416 for (pos=str; *pos; pos++)
3417 *pos = *pos == orig ? new : *pos;
3418
3419 return str;
3420 }
3421
3422 static gboolean write_escrow_data_file (struct libvk_volume *volume, struct libvk_ui *ui, enum libvk_secret secret_type, const gchar *out_path,
3423 CERTCertificate *cert, GError **error) {
3424 gpointer packet_data = NULL;
3425 gsize packet_data_size = 0;
3426 GIOChannel *out_file = NULL;
3427 GIOStatus status = G_IO_STATUS_ERROR;
3428 gsize bytes_written = 0;
3429 GError *l_error = NULL;
3430
3431 packet_data = libvk_volume_create_packet_asymmetric_with_format (volume, &packet_data_size, secret_type, cert,
3432 ui, LIBVK_PACKET_FORMAT_ASYMMETRIC_WRAP_SECRET_ONLY, &l_error);
3433 if (!packet_data) {
3434 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_ESCROW_FAILED,
3435 "Failed to get escrow data: %s", l_error->message);
3436 g_clear_error (&l_error);
3437 return FALSE;
3438 }
3439
3440 out_file = g_io_channel_new_file (out_path, "w", error);
3441 if (!out_file) {
3442 /* error is already populated */
3443 g_free (packet_data);
3444 return FALSE;
3445 }
3446
3447 status = g_io_channel_set_encoding (out_file, NULL, error);
3448 if (status != G_IO_STATUS_NORMAL) {
3449 g_free (packet_data);
3450
3451 /* try to shutdown the channel, but if it fails, we cannot do anything about it here */
3452 g_io_channel_shutdown (out_file, TRUE, NULL);
3453
3454 /* error is already populated */
3455 g_io_channel_unref (out_file);
3456 return FALSE;
3457 }
3458
3459 status = g_io_channel_write_chars (out_file, (const gchar *) packet_data, (gssize) packet_data_size,
3460 &bytes_written, error);
3461 g_free (packet_data);
3462 if (status != G_IO_STATUS_NORMAL) {
3463 /* try to shutdown the channel, but if it fails, we cannot do anything about it here */
3464 g_io_channel_shutdown (out_file, TRUE, NULL);
3465
3466 /* error is already populated */
3467 g_io_channel_unref (out_file);
3468 return FALSE;
3469 }
3470
3471 if (g_io_channel_shutdown (out_file, TRUE, error) != G_IO_STATUS_NORMAL) {
3472 /* error is already populated */
3473 g_io_channel_unref (out_file);
3474 return FALSE;
3475 }
3476 g_io_channel_unref (out_file);
3477
3478 return TRUE;
3479 }
3480 #endif /* WITH_BD_ESCROW */
3481
3482 /**
3483 * bd_crypto_escrow_device:
3484 * @device: path of the device to create escrow data for
3485 * @passphrase: passphrase used for the device
3486 * @cert_data: (array zero-terminated=1) (element-type gchar): certificate data to use for escrow
3487 * @directory: directory to put escrow data into
3488 * @backup_passphrase: (nullable): backup passphrase for the device or %NULL
3489 * @error: (out) (optional): place to store error (if any)
3490 *
3491 * Returns: whether the escrow data was successfully created for @device or not
3492 *
3493 * Tech category: %BD_CRYPTO_TECH_ESCROW-%BD_CRYPTO_TECH_MODE_CREATE
3494 */
3495 #ifndef WITH_BD_ESCROW
3496 gboolean bd_crypto_escrow_device (const gchar *device G_GNUC_UNUSED, const gchar *passphrase G_GNUC_UNUSED,const gchar *cert_data G_GNUC_UNUSED,
3497 const gchar *directory G_GNUC_UNUSED, const gchar *backup_passphrase G_GNUC_UNUSED, GError **error) {
3498 /* this will return FALSE and set error, because escrow technology is not available */
3499 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_ESCROW, BD_CRYPTO_TECH_MODE_CREATE, error);
3500 }
3501 #else
3502 gboolean bd_crypto_escrow_device (const gchar *device, const gchar *passphrase, const gchar *cert_data, const gchar *directory, const gchar *backup_passphrase, GError **error) {
3503 struct libvk_volume *volume = NULL;
3504 struct libvk_ui *ui = NULL;
3505 gchar *label = NULL;
3506 gchar *uuid = NULL;
3507 CERTCertificate *cert = NULL;
3508 gchar *volume_ident = NULL;
3509 gchar *out_path = NULL;
3510 gboolean ret = FALSE;
3511 gchar *passphrase_copy = NULL;
3512 gchar *cert_data_copy = NULL;
3513 guint64 progress_id = 0;
3514 gchar *msg = NULL;
3515 GError *l_error = NULL;
3516
3517 msg = g_strdup_printf ("Started creating escrow data for the LUKS device '%s'", device);
3518 progress_id = bd_utils_report_started (msg);
3519 g_free (msg);
3520
3521 if (!NSS_IsInitialized ())
3522 if (NSS_NoDB_Init (NULL) != SECSuccess) {
3523 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_NSS_INIT_FAILED,
3524 "Failed to initialize NSS");
3525 bd_utils_report_finished (progress_id, l_error->message);
3526 g_propagate_error (error, l_error);
3527 return FALSE;
3528 }
3529
3530 volume = libvk_volume_open (device, &l_error);
3531 if (!volume) {
3532 bd_utils_report_finished (progress_id, l_error->message);
3533 g_propagate_error (error, l_error);
3534 return FALSE;
3535 }
3536
3537 ui = libvk_ui_new ();
3538 /* not supposed to be called -> always fail */
3539 libvk_ui_set_generic_cb (ui, always_fail_cb, NULL, NULL);
3540
3541 /* Create a copy of the passphrase to be used by the passphrase callback.
3542 * The passphrase will be freed by volume_key via the free callback.
3543 */
3544 passphrase_copy = g_strdup (passphrase);
3545 libvk_ui_set_passphrase_cb (ui, give_passphrase_cb, passphrase_copy, free_passphrase_cb);
3546
3547 if (libvk_volume_get_secret (volume, LIBVK_SECRET_DEFAULT, ui, &l_error) != 0) {
3548 /* error is already populated */
3549 libvk_volume_free (volume);
3550 libvk_ui_free (ui);
3551 bd_utils_report_finished (progress_id, l_error->message);
3552 g_propagate_error (error, l_error);
3553 return FALSE;
3554 }
3555
3556 cert_data_copy = g_strdup (cert_data);
3557 cert = CERT_DecodeCertFromPackage (cert_data_copy, strlen (cert_data_copy));
3558 if (!cert) {
3559 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_CERT_DECODE,
3560 "Failed to decode the certificate data");
3561 libvk_volume_free (volume);
3562 libvk_ui_free (ui);
3563 g_free (cert_data_copy);
3564 bd_utils_report_finished (progress_id, l_error->message);
3565 g_propagate_error (error, l_error);
3566 return FALSE;
3567 }
3568
3569 label = libvk_volume_get_label (volume);
3570 replace_char (label, '/', '_');
3571 uuid = libvk_volume_get_uuid (volume);
3572 replace_char (uuid, '/', '_');
3573
3574 if (label && uuid) {
3575 volume_ident = g_strdup_printf ("%s-%s", label, uuid);
3576 g_free (label);
3577 g_free (uuid);
3578 } else if (uuid)
3579 volume_ident = uuid;
3580 else
3581 volume_ident = g_strdup ("_unknown");
3582
3583 out_path = g_strdup_printf ("%s/%s-escrow", directory, volume_ident);
3584 ret = write_escrow_data_file (volume, ui, LIBVK_SECRET_DEFAULT, out_path, cert, &l_error);
3585 g_free (out_path);
3586
3587 if (!ret) {
3588 CERT_DestroyCertificate (cert);
3589 libvk_volume_free (volume);
3590 libvk_ui_free (ui);
3591 g_free (volume_ident);
3592 g_free (cert_data_copy);
3593 bd_utils_report_finished (progress_id, l_error->message);
3594 g_propagate_error (error, l_error);
3595 return FALSE;
3596 }
3597
3598 if (backup_passphrase) {
3599 if (libvk_volume_add_secret (volume, LIBVK_SECRET_PASSPHRASE, backup_passphrase, strlen (backup_passphrase), &l_error) != 0) {
3600 /* error is already populated */
3601 CERT_DestroyCertificate (cert);
3602 libvk_volume_free (volume);
3603 libvk_ui_free (ui);
3604 g_free (volume_ident);
3605 g_free (cert_data_copy);
3606 bd_utils_report_finished (progress_id, l_error->message);
3607 g_propagate_error (error, l_error);
3608 return FALSE;
3609 }
3610
3611 out_path = g_strdup_printf ("%s/%s-escrow-backup-passphrase", directory, volume_ident);
3612 ret = write_escrow_data_file (volume, ui, LIBVK_SECRET_PASSPHRASE, out_path, cert, &l_error);
3613 g_free (out_path);
3614
3615 if (!ret) {
3616 CERT_DestroyCertificate (cert);
3617 libvk_volume_free (volume);
3618 libvk_ui_free (ui);
3619 g_free (volume_ident);
3620 g_free (cert_data_copy);
3621 bd_utils_report_finished (progress_id, l_error->message);
3622 g_propagate_error (error, l_error);
3623 return FALSE;
3624 }
3625 }
3626
3627 CERT_DestroyCertificate (cert);
3628 libvk_volume_free (volume);
3629 libvk_ui_free (ui);
3630 g_free (volume_ident);
3631 g_free (cert_data_copy);
3632 bd_utils_report_finished (progress_id, "Completed");
3633 return ret;
3634 }
3635 #endif /* WITH_BD_ESCROW */
3636
3637 /**
3638 * bd_crypto_bitlk_open_flags:
3639 * @device: the device to open
3640 * @name: name for the BITLK device
3641 * @context: key slot context (passphrase/keyfile/token...) for this BITLK device
3642 * @flags: activation flags for the BITLK device
3643 * @error: (out) (optional): place to store error (if any)
3644 *
3645 * Supported @context types for this function: passphrase, key file
3646 *
3647 * Returns: whether the @device was successfully opened or not
3648 *
3649 * Tech category: %BD_CRYPTO_TECH_BITLK-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3650 */
3651 gboolean bd_crypto_bitlk_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, BDCryptoOpenFlags flags, GError **error) {
3652 struct crypt_device *cd = NULL;
3653 gint ret = 0;
3654 guint64 progress_id = 0;
3655 gchar *msg = NULL;
3656 GError *l_error = NULL;
3657 gchar *key_buffer = NULL;
3658 gsize buf_len = 0;
3659 guint32 crypt_flags = 0;
3660
3661 if (!_is_dm_name_valid (name, error))
3662 return FALSE;
3663
3664 msg = g_strdup_printf ("Started opening '%s' BITLK device", device);
3665 progress_id = bd_utils_report_started (msg);
3666 g_free (msg);
3667
3668 ret = crypt_init (&cd, device);
3669 if (ret != 0) {
3670 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3671 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
3672 bd_utils_report_finished (progress_id, l_error->message);
3673 g_propagate_error (error, l_error);
3674 return FALSE;
3675 }
3676
3677 ret = crypt_load (cd, CRYPT_BITLK, NULL);
3678 if (ret != 0) {
3679 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3680 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
3681 crypt_free (cd);
3682 bd_utils_report_finished (progress_id, l_error->message);
3683 g_propagate_error (error, l_error);
3684 return FALSE;
3685 }
3686
3687 if (flags & BD_CRYPTO_OPEN_ALLOW_DISCARDS)
3688 crypt_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
3689 if (flags & BD_CRYPTO_OPEN_READONLY)
3690 crypt_flags |= CRYPT_ACTIVATE_READONLY;
3691
3692 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
3693 ret = crypt_activate_by_passphrase (cd, name, CRYPT_ANY_SLOT,
3694 (const char *) context->u.passphrase.pass_data,
3695 context->u.passphrase.data_len,
3696 crypt_flags);
3697 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
3698 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buffer, &buf_len,
3699 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
3700 if (ret != 0) {
3701 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
3702 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile, strerror_l (-ret, c_locale));
3703 crypt_free (cd);
3704 bd_utils_report_finished (progress_id, l_error->message);
3705 g_propagate_error (error, l_error);
3706 return FALSE;
3707 }
3708 ret = crypt_activate_by_passphrase (cd, name, CRYPT_ANY_SLOT, key_buffer, buf_len, crypt_flags);
3709 crypt_safe_free (key_buffer);
3710 } else {
3711 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
3712 "Only 'passphrase' and 'key file' context types are valid for BITLK open.");
3713 bd_utils_report_finished (progress_id, l_error->message);
3714 g_propagate_error (error, l_error);
3715 crypt_free (cd);
3716 return FALSE;
3717 }
3718
3719 if (ret < 0) {
3720 if (ret == -EPERM)
3721 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3722 "Failed to activate device: Incorrect passphrase.");
3723 else
3724 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3725 "Failed to activate device: %s", strerror_l (-ret, c_locale));
3726
3727 crypt_free (cd);
3728 bd_utils_report_finished (progress_id, l_error->message);
3729 g_propagate_error (error, l_error);
3730 return FALSE;
3731 }
3732
3733 crypt_free (cd);
3734 bd_utils_report_finished (progress_id, "Completed");
3735 return TRUE;
3736 }
3737
3738 /**
3739 * bd_crypto_bitlk_open:
3740 * @device: the device to open
3741 * @name: name for the BITLK device
3742 * @context: key slot context (passphrase/keyfile/token...) for this BITLK device
3743 * @read_only: whether to open as read-only or not (meaning read-write)
3744 * @error: (out) (optional): place to store error (if any)
3745 *
3746 * Supported @context types for this function: passphrase, key file
3747 *
3748 * Returns: whether the @device was successfully opened or not
3749 *
3750 * Tech category: %BD_CRYPTO_TECH_BITLK-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3751 */
3752 gboolean bd_crypto_bitlk_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, gboolean read_only, GError **error) {
3753 return bd_crypto_bitlk_open_flags (device, name, context, read_only ? BD_CRYPTO_OPEN_READONLY : 0, error);
3754 }
3755
3756 /**
3757 * bd_crypto_bitlk_close:
3758 * @bitlk_device: BITLK device to close
3759 * @error: (out) (optional): place to store error (if any)
3760 *
3761 * Returns: whether the given @bitlk_device was successfully closed or not
3762 *
3763 * Tech category: %BD_CRYPTO_TECH_BITLK-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3764 */
3765 gboolean bd_crypto_bitlk_close (const gchar *bitlk_device, GError **error) {
3766 return _crypto_close (bitlk_device, "BITLK", error);
3767 }
3768
3769 /**
3770 * bd_crypto_fvault2_open_flags:
3771 * @device: the device to open
3772 * @name: name for the FVAULT2 device
3773 * @context: key slot context (passphrase/keyfile/token...) for this FVAULT2 volume
3774 * @flags: activation flags for the FVAULT2 device
3775 * @error: (out) (optional): place to store error (if any)
3776 *
3777 * Supported @context types for this function: passphrase, key file
3778 *
3779 * Returns: whether the @device was successfully opened or not
3780 *
3781 * Tech category: %BD_CRYPTO_TECH_FVAULT2-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3782 */
3783 #ifndef LIBCRYPTSETUP_26
3784 gboolean bd_crypto_fvault2_open_flags (const gchar *device G_GNUC_UNUSED, const gchar *name G_GNUC_UNUSED, BDCryptoKeyslotContext *context G_GNUC_UNUSED,
3785 BDCryptoOpenFlags flags G_GNUC_UNUSED, GError **error) {
3786 /* this will return FALSE and set error, because FVAULT2 technology is not available */
3787 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_FVAULT2, BD_CRYPTO_TECH_MODE_OPEN_CLOSE, error);
3788 #else
3789 gboolean bd_crypto_fvault2_open_flags (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, BDCryptoOpenFlags flags, GError **error) {
3790 struct crypt_device *cd = NULL;
3791 gint ret = 0;
3792 guint64 progress_id = 0;
3793 gchar *msg = NULL;
3794 GError *l_error = NULL;
3795 gchar *key_buffer = NULL;
3796 gsize buf_len = 0;
3797 guint32 crypt_flags = 0;
3798
3799 if (!_is_dm_name_valid (name, error))
3800 return FALSE;
3801
3802 msg = g_strdup_printf ("Started opening '%s' FVAULT2 device", device);
3803 progress_id = bd_utils_report_started (msg);
3804 g_free (msg);
3805
3806 ret = crypt_init (&cd, device);
3807 if (ret != 0) {
3808 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3809 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
3810 bd_utils_report_finished (progress_id, l_error->message);
3811 g_propagate_error (error, l_error);
3812 return FALSE;
3813 }
3814
3815 ret = crypt_load (cd, CRYPT_FVAULT2, NULL);
3816 if (ret != 0) {
3817 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3818 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
3819 crypt_free (cd);
3820 bd_utils_report_finished (progress_id, l_error->message);
3821 g_propagate_error (error, l_error);
3822 return FALSE;
3823 }
3824
3825 if (flags & BD_CRYPTO_OPEN_ALLOW_DISCARDS)
3826 crypt_flags |= CRYPT_ACTIVATE_ALLOW_DISCARDS;
3827 if (flags & BD_CRYPTO_OPEN_READONLY)
3828 crypt_flags |= CRYPT_ACTIVATE_READONLY;
3829
3830 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
3831 ret = crypt_activate_by_passphrase (cd, name, CRYPT_ANY_SLOT,
3832 (const char *) context->u.passphrase.pass_data,
3833 context->u.passphrase.data_len,
3834 crypt_flags);
3835 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
3836 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buffer, &buf_len,
3837 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
3838 if (ret != 0) {
3839 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
3840 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile, strerror_l (-ret, c_locale));
3841 crypt_free (cd);
3842 bd_utils_report_finished (progress_id, l_error->message);
3843 g_propagate_error (error, l_error);
3844 return FALSE;
3845 }
3846 ret = crypt_activate_by_passphrase (cd, name, CRYPT_ANY_SLOT, key_buffer, buf_len, crypt_flags);
3847 crypt_safe_free (key_buffer);
3848 } else {
3849 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
3850 "Only 'passphrase' and 'key file' context types are valid for FVAULT2 open.");
3851 bd_utils_report_finished (progress_id, l_error->message);
3852 g_propagate_error (error, l_error);
3853 crypt_free (cd);
3854 return FALSE;
3855 }
3856
3857 if (ret < 0) {
3858 if (ret == -EPERM)
3859 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3860 "Failed to activate device: Incorrect passphrase.");
3861 else
3862 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3863 "Failed to activate device: %s", strerror_l (-ret, c_locale));
3864
3865 crypt_free (cd);
3866 bd_utils_report_finished (progress_id, l_error->message);
3867 g_propagate_error (error, l_error);
3868 return FALSE;
3869 }
3870
3871 crypt_free (cd);
3872 bd_utils_report_finished (progress_id, "Completed");
3873 return TRUE;
3874 #endif
3875 }
3876
3877 /**
3878 * bd_crypto_fvault2_open:
3879 * @device: the device to open
3880 * @name: name for the FVAULT2 device
3881 * @context: key slot context (passphrase/keyfile/token...) for this FVAULT2 volume
3882 * @read_only: whether to open as read-only or not (meaning read-write)
3883 * @error: (out) (optional): place to store error (if any)
3884 *
3885 * Supported @context types for this function: passphrase, key file
3886 *
3887 * Returns: whether the @device was successfully opened or not
3888 *
3889 * Tech category: %BD_CRYPTO_TECH_FVAULT2-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3890 */
3891 #ifndef LIBCRYPTSETUP_26
3892 gboolean bd_crypto_fvault2_open (const gchar *device G_GNUC_UNUSED, const gchar *name G_GNUC_UNUSED, BDCryptoKeyslotContext *context G_GNUC_UNUSED,
3893 gboolean read_only G_GNUC_UNUSED, GError **error) {
3894 /* this will return FALSE and set error, because FVAULT2 technology is not available */
3895 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_FVAULT2, BD_CRYPTO_TECH_MODE_OPEN_CLOSE, error);
3896 }
3897 #else
3898 gboolean bd_crypto_fvault2_open (const gchar *device, const gchar *name, BDCryptoKeyslotContext *context, gboolean read_only, GError **error) {
3899 return bd_crypto_fvault2_open_flags (device, name, context, read_only ? BD_CRYPTO_OPEN_READONLY : 0, error);
3900 }
3901 #endif
3902
3903 /**
3904 * bd_crypto_fvault2_close:
3905 * @fvault2_device: FVAULT2 device to close
3906 * @error: (out) (optional): place to store error (if any)
3907 *
3908 * Returns: whether the given @fvault2_device was successfully closed or not
3909 *
3910 * Tech category: %BD_CRYPTO_TECH_FVAULT2-%BD_CRYPTO_TECH_MODE_OPEN_CLOSE
3911 */
3912 #ifndef LIBCRYPTSETUP_26
3913 gboolean bd_crypto_fvault2_close (const gchar *fvault2_device G_GNUC_UNUSED, GError **error) {
3914 /* this will return FALSE and set error, because FVAULT2 technology is not available */
3915 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_FVAULT2, BD_CRYPTO_TECH_MODE_OPEN_CLOSE, error);
3916 }
3917 #else
3918 gboolean bd_crypto_fvault2_close (const gchar *fvault2_device, GError **error) {
3919 return _crypto_close (fvault2_device, "FVAULT2", error);
3920 }
3921 #endif
3922
3923 /**
3924 * bd_crypto_opal_is_supported:
3925 * @device: device to check for OPAL support
3926 * @error: (out) (optional): place to store error (if any)
3927 *
3928 * Returns: %TRUE if the given @device supports OPAL or %FALSE if not or
3929 * failed to determine (the @error is populated with the error in such
3930 * cases).
3931 *
3932 * Tech category: %BD_CRYPTO_TECH_SED_OPAL-%BD_CRYPTO_TECH_MODE_QUERY
3933 */
3934 #if !defined(HAVE_LINUX_OPAL) || !defined(IOC_OPAL_GET_STATUS)
3935 gboolean bd_crypto_opal_is_supported (const gchar *device G_GNUC_UNUSED, GError **error) {
3936 /* this will return FALSE and set error, because OPAL technology is not available */
3937 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_SED_OPAL, BD_CRYPTO_TECH_MODE_QUERY, error);
3938 }
3939 #else
3940 gboolean bd_crypto_opal_is_supported (const gchar *device, GError **error) {
3941 gint fd = -1;
3942 gint ret = 0;
3943 struct opal_status st = ZERO_INIT;
3944
3945 fd = open (device, O_RDONLY|O_CLOEXEC);
3946 if (fd == -1) {
3947 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3948 "Failed to open the device '%s'", device);
3949 return FALSE;
3950 }
3951
3952 ret = ioctl (fd, IOC_OPAL_GET_STATUS, &st);
3953 if (ret < 0) {
3954 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
3955 "Failed to get opal status for the device '%s': %s",
3956 device,
3957 strerror_l (errno, c_locale));
3958 close (fd);
3959 return FALSE;
3960 }
3961
3962 close (fd);
3963
3964 if (st.flags & (OPAL_FL_SUPPORTED|OPAL_FL_LOCKING_SUPPORTED|OPAL_FL_LOCKING_ENABLED))
3965 return TRUE;
3966 else
3967 return FALSE;
3968 }
3969 #endif
3970
3971 /**
3972 * bd_crypto_opal_wipe_device:
3973 * @device: LUKS HW-OPAL device to wipe
3974 * @context: OPAL admin passphrase context
3975 * @error: (out) (optional): place to store error (if any)
3976 *
3977 * Returns: whether @device was successfully wiped or not.
3978 *
3979 * Supported @context types for this function: passphrase
3980 *
3981 * Tech category: %BD_CRYPTO_TECH_SED_OPAL-%BD_CRYPTO_TECH_MODE_MODIFY
3982 */
3983 #ifndef LIBCRYPTSETUP_27
3984 gboolean bd_crypto_opal_wipe_device (const gchar *device G_GNUC_UNUSED, BDCryptoKeyslotContext *context G_GNUC_UNUSED, GError **error) {
3985 /* this will return FALSE and set error, because OPAL technology is not available */
3986 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_SED_OPAL, BD_CRYPTO_TECH_MODE_QUERY, error);
3987 }
3988 #else
3989 gboolean bd_crypto_opal_wipe_device (const gchar *device, BDCryptoKeyslotContext *context, GError **error) {
3990 gchar *key_buf = NULL;
3991 gsize buf_len = 0;
3992 struct crypt_device *cd = NULL;
3993 gint ret = 0;
3994 guint64 progress_id = 0;
3995 GError *l_error = NULL;
3996 gchar *msg = NULL;
3997
3998 msg = g_strdup_printf ("Started wiping '%s' LUKS HW-OPAL device", device);
3999 progress_id = bd_utils_report_started (msg);
4000 g_free (msg);
4001
4002 ret = crypt_init (&cd, device);
4003 if (ret != 0) {
4004 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
4005 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
4006 bd_utils_report_finished (progress_id, l_error->message);
4007 g_propagate_error (error, l_error);
4008 return FALSE;
4009 }
4010
4011 ret = crypt_load (cd, CRYPT_LUKS, NULL);
4012 if (ret != 0) {
4013 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
4014 "Failed to load device's parameters: %s", strerror_l (-ret, c_locale));
4015 crypt_free (cd);
4016 bd_utils_report_finished (progress_id, l_error->message);
4017 g_propagate_error (error, l_error);
4018 return FALSE;
4019 }
4020
4021 ret = crypt_get_hw_encryption_type (cd);
4022 if (ret != CRYPT_OPAL_HW_ONLY && ret != CRYPT_SW_AND_OPAL_HW) {
4023 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
4024 "Device %s isn't a LUKS HW-OPAL device.", device);
4025 bd_utils_report_finished (progress_id, l_error->message);
4026 g_propagate_error (error, l_error);
4027 crypt_free (cd);
4028 return FALSE;
4029 }
4030
4031 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
4032 key_buf = (char *) context->u.passphrase.pass_data;
4033 buf_len = context->u.passphrase.data_len;
4034 } else {
4035 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
4036 "Only 'passphrase' context type is valid for OPAL wipe.");
4037 bd_utils_report_finished (progress_id, l_error->message);
4038 g_propagate_error (error, l_error);
4039 crypt_free (cd);
4040 return FALSE;
4041 }
4042
4043 ret = crypt_wipe_hw_opal (cd, CRYPT_LUKS2_SEGMENT, key_buf, buf_len, 0);
4044 if (ret != 0) {
4045 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
4046 "Failed to wipe LUKS HW-OPAL device: %s", strerror_l (-ret, c_locale));
4047 crypt_free (cd);
4048 bd_utils_report_finished (progress_id, l_error->message);
4049 g_propagate_error (error, l_error);
4050 return FALSE;
4051 }
4052
4053 crypt_free (cd);
4054 bd_utils_report_finished (progress_id, "Completed");
4055 return TRUE;
4056 }
4057 #endif
4058
4059 /**
4060 * bd_crypto_opal_format:
4061 * @device: a device to format as LUKS HW-OPAL
4062 * @cipher: (nullable): cipher specification (type-mode, e.g. "aes-xts-plain64") or %NULL to use the default
4063 * @key_size: size of the volume key in bits or 0 to use the default
4064 * @context: key slot context (passphrase/keyfile/token...) for this LUKS device
4065 * @min_entropy: minimum random data entropy (in bits) required to format @device as LUKS
4066 * @hw_encryption: type of hardware encryption (SW+HW or HW only)
4067 * @opal_context: OPAL admin passphrase
4068 * @extra: (nullable): extra arguments for LUKS format creation
4069 * @error: (out) (optional): place to store error (if any)
4070 *
4071 * Formats the given @device as LUKS HW-OPAL according to the other parameters given. If
4072 * @min_entropy is specified (greater than 0), the function waits for enough
4073 * entropy to be available in the random data pool (WHICH MAY POTENTIALLY TAKE
4074 * FOREVER).
4075 *
4076 * Supported @context types for this function: passphrase, key file
4077 * Supported @opal_context types for this function: passphrase
4078 *
4079 * Returns: whether the given @device was successfully formatted as LUKS HW-OPAL or not
4080 * (the @error contains the error in such cases)
4081 *
4082 * Tech category: %BD_CRYPTO_TECH_LUKS-%BD_CRYPTO_TECH_MODE_CREATE
4083 */
4084 #ifndef LIBCRYPTSETUP_27
4085 gboolean bd_crypto_opal_format (const gchar *device G_GNUC_UNUSED, const gchar *cipher G_GNUC_UNUSED, guint64 key_size G_GNUC_UNUSED, BDCryptoKeyslotContext *context G_GNUC_UNUSED,
4086 guint64 min_entropy G_GNUC_UNUSED, BDCryptoLUKSHWEncryptionType hw_encryption G_GNUC_UNUSED,
4087 BDCryptoKeyslotContext *opal_context G_GNUC_UNUSED, BDCryptoLUKSExtra *extra G_GNUC_UNUSED, GError **error) {
4088 /* this will return FALSE and set error, because OPAL technology is not available */
4089 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_SED_OPAL, BD_CRYPTO_TECH_MODE_CREATE, error);
4090 }
4091 #else
4092 gboolean bd_crypto_opal_format (const gchar *device, const gchar *cipher, guint64 key_size, BDCryptoKeyslotContext *context, guint64 min_entropy, BDCryptoLUKSHWEncryptionType hw_encryption,
4093 BDCryptoKeyslotContext *opal_context, BDCryptoLUKSExtra *extra, GError **error) {
4094
4095 gboolean ret = FALSE;
4096
4097 if (hw_encryption != BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_AND_SW && hw_encryption != BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_ONLY) {
4098 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
4099 "Either hardware and software encryption or hardware encryption only must be selected for OPAL format");
4100 return FALSE;
4101 }
4102
4103 if (hw_encryption == BD_CRYPTO_LUKS_HW_ENCRYPTION_OPAL_HW_ONLY && cipher != NULL) {
4104 g_set_error_literal (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
4105 "Cipher cannot be specified for hardware encryption only OPAL devices");
4106 return FALSE;
4107 }
4108
4109 ret = bd_crypto_opal_is_supported (device, NULL);
4110 if (!ret) {
4111 g_set_error (error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_FORMAT_FAILED,
4112 "OPAL doesn't seem to be supported on %s", device);
4113 return FALSE;
4114 }
4115
4116 return _crypto_luks_format (device, cipher, key_size, context, min_entropy, BD_CRYPTO_LUKS_VERSION_LUKS2, extra, hw_encryption, opal_context, error);
4117 }
4118 #endif
4119
4120 /**
4121 * bd_crypto_opal_reset_device:
4122 * @device: LUKS HW-OPAL device to run PSID reset on
4123 * @context: PSID context
4124 * @error: (out) (optional): place to store error (if any)
4125 *
4126 * Returns: whether PSI reset on @device was successful or not.
4127 *
4128 * Warning: PSID reset will remove all data from @device!
4129 *
4130 * Supported @context types for this function: passphrase, key file
4131 *
4132 * Tech category: %BD_CRYPTO_TECH_SED_OPAL-%BD_CRYPTO_TECH_MODE_MODIFY
4133 */
4134 #ifndef LIBCRYPTSETUP_27
4135 gboolean bd_crypto_opal_reset_device (const gchar *device G_GNUC_UNUSED, BDCryptoKeyslotContext *context G_GNUC_UNUSED, GError **error) {
4136 /* this will return FALSE and set error, because OPAL technology is not available */
4137 return bd_crypto_is_tech_avail (BD_CRYPTO_TECH_SED_OPAL, BD_CRYPTO_TECH_MODE_QUERY, error);
4138 }
4139 #else
4140 gboolean bd_crypto_opal_reset_device (const gchar *device, BDCryptoKeyslotContext *context, GError **error) {
4141 gchar *key_buf = NULL;
4142 gsize buf_len = 0;
4143 struct crypt_device *cd = NULL;
4144 gint ret = 0;
4145 guint64 progress_id = 0;
4146 GError *l_error = NULL;
4147 gchar *msg = NULL;
4148
4149 msg = g_strdup_printf ("Started PSID reset on '%s' LUKS HW-OPAL device", device);
4150 progress_id = bd_utils_report_started (msg);
4151 g_free (msg);
4152
4153 if (!bd_crypto_opal_is_supported (device, &l_error)) {
4154 g_prefix_error (&l_error, "OPAL doesn't seem to be supported on %s: ", device);
4155 bd_utils_report_finished (progress_id, l_error->message);
4156 g_propagate_error (error, l_error);
4157 return FALSE;
4158 }
4159
4160 ret = crypt_init (&cd, device);
4161 if (ret != 0) {
4162 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
4163 "Failed to initialize device: %s", strerror_l (-ret, c_locale));
4164 bd_utils_report_finished (progress_id, l_error->message);
4165 g_propagate_error (error, l_error);
4166 return FALSE;
4167 }
4168
4169 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_PASSPHRASE) {
4170 key_buf = (char *) context->u.passphrase.pass_data;
4171 buf_len = context->u.passphrase.data_len;
4172 } else if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE) {
4173 ret = crypt_keyfile_device_read (cd, context->u.keyfile.keyfile, &key_buf, &buf_len,
4174 context->u.keyfile.keyfile_offset, context->u.keyfile.key_size, 0);
4175 if (ret != 0) {
4176 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_KEYFILE_FAILED,
4177 "Failed to read key from file '%s': %s", context->u.keyfile.keyfile,
4178 strerror_l (-ret, c_locale));
4179 crypt_free (cd);
4180 bd_utils_report_finished (progress_id, l_error->message);
4181 g_propagate_error (error, l_error);
4182 return FALSE;
4183 }
4184 } else {
4185 g_set_error_literal (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_INVALID_CONTEXT,
4186 "Only 'passphrase' and 'key file' context types are valid for OPAL PSID reset.");
4187 bd_utils_report_finished (progress_id, l_error->message);
4188 g_propagate_error (error, l_error);
4189 crypt_free (cd);
4190 return FALSE;
4191 }
4192
4193 ret = crypt_wipe_hw_opal (cd, CRYPT_NO_SEGMENT, key_buf, buf_len, 0);
4194 if (context->type == BD_CRYPTO_KEYSLOT_CONTEXT_TYPE_KEYFILE)
4195 crypt_safe_free (key_buf);
4196
4197 if (ret != 0) {
4198 g_set_error (&l_error, BD_CRYPTO_ERROR, BD_CRYPTO_ERROR_DEVICE,
4199 "Failed to wipe LUKS HW-OPAL device: %s", strerror_l (-ret, c_locale));
4200 crypt_free (cd);
4201 bd_utils_report_finished (progress_id, l_error->message);
4202 g_propagate_error (error, l_error);
4203 return FALSE;
4204 }
4205
4206 crypt_free (cd);
4207 bd_utils_report_finished (progress_id, "Completed");
4208 return TRUE;
4209 }
4210 #endif