1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
|
import unittest
import os
import tempfile
import overrides_hack
import shutil
import subprocess
import six
import locale
import re
import tarfile
from utils import create_sparse_tempfile, create_lio_device, delete_lio_device, skip_on, get_avail_locales, requires_locales, run_command, read_file
from gi.repository import BlockDev, GLib
PASSWD = "myshinylittlepassword"
PASSWD2 = "myshinylittlepassword2"
PASSWD3 = "myshinylittlepassword3"
def have_luks2():
try:
succ = BlockDev.utils_check_util_version("cryptsetup", "2.0.3", "--version", r"cryptsetup ([0-9+\.]+)")
except GLib.GError:
return False
else:
return succ
HAVE_LUKS2 = have_luks2()
class CryptoTestCase(unittest.TestCase):
requested_plugins = BlockDev.plugin_specs_from_names(("crypto", "loop"))
@classmethod
def setUpClass(cls):
unittest.TestCase.setUpClass()
cls.avail_locales = get_avail_locales()
if not BlockDev.is_initialized():
BlockDev.init(cls.requested_plugins, None)
else:
BlockDev.reinit(cls.requested_plugins, True, None)
def setUp(self):
self.addCleanup(self._clean_up)
self.dev_file = create_sparse_tempfile("crypto_test", 1024**3)
self.dev_file2 = create_sparse_tempfile("crypto_test2", 1024**3)
try:
self.loop_dev = create_lio_device(self.dev_file)
except RuntimeError as e:
raise RuntimeError("Failed to setup loop device for testing: %s" % e)
try:
self.loop_dev2 = create_lio_device(self.dev_file2)
except RuntimeError as e:
raise RuntimeError("Failed to setup loop device for testing: %s" % e)
# make a key file
handle, self.keyfile = tempfile.mkstemp(prefix="libblockdev_test_keyfile", text=False)
os.write(handle, b"nobodyknows")
os.close(handle)
def _clean_up(self):
try:
BlockDev.crypto_luks_close("libblockdevTestLUKS")
except:
pass
try:
delete_lio_device(self.loop_dev)
except RuntimeError:
# just move on, we can do no better here
pass
os.unlink(self.dev_file)
try:
delete_lio_device(self.loop_dev2)
except RuntimeError:
# just move on, we can do no better here
pass
os.unlink(self.dev_file2)
os.unlink(self.keyfile)
def _luks_format(self, device, passphrase, keyfile):
return BlockDev.crypto_luks_format(device, None, 0, passphrase, keyfile, 0)
def _luks2_format(self, device, passphrase, keyfile):
return BlockDev.crypto_luks_format(device, None, 0, passphrase, keyfile, 0, BlockDev.CryptoLUKSVersion.LUKS2, None)
class CryptoTestGenerateBackupPassphrase(CryptoTestCase):
def setUp(self):
# we don't need block devices for this test
pass
def test_generate_backup_passhprase(self):
"""Verify that backup passphrase generation works as expected"""
exp = r"^([0-9A-Za-z./]{5}-){3}[0-9A-Za-z./]{5}$"
for _i in range(100):
bp = BlockDev.crypto_generate_backup_passphrase()
six.assertRegex(self, bp, exp)
class CryptoTestFormat(CryptoTestCase):
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_format(self):
"""Verify that formating device as LUKS works"""
# no passphrase nor keyfile
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_format(self.loop_dev, None, 0, None, None, 0)
# the simple case with password
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, PASSWD, None, 0)
self.assertTrue(succ)
# create with a keyfile
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, None, self.keyfile, 0)
self.assertTrue(succ)
# the simple case with password blob
succ = BlockDev.crypto_luks_format_blob(self.loop_dev, "aes-xts-plain64", 0, [ord(c) for c in PASSWD], 0)
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_format(self):
"""Verify that formating device as LUKS 2 works"""
# no passphrase nor keyfile
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_format(self.loop_dev, None, 0, None, None, 0)
# the simple case with password
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, PASSWD, None, 0)
self.assertTrue(succ)
# create with a keyfile
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, None, self.keyfile, 0)
self.assertTrue(succ)
# the simple case with password blob
succ = BlockDev.crypto_luks_format_blob(self.loop_dev, "aes-xts-plain64", 0, [ord(c) for c in PASSWD], 0)
self.assertTrue(succ)
# simple case with extra options
extra = BlockDev.CryptoLUKSExtra(label="blockdevLUKS")
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, None, self.keyfile, 0,
BlockDev.CryptoLUKSVersion.LUKS2, extra)
self.assertTrue(succ)
_ret, label, _err = run_command("lsblk -oLABEL -n %s" % self.loop_dev)
self.assertEqual(label, "blockdevLUKS")
# different key derivation function
pbkdf = BlockDev.CryptoLUKSPBKDF(type="pbkdf2")
extra = BlockDev.CryptoLUKSExtra(pbkdf=pbkdf)
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, None, self.keyfile, 0,
BlockDev.CryptoLUKSVersion.LUKS2, extra)
self.assertTrue(succ)
_ret, out, err = run_command("cryptsetup luksDump %s" % self.loop_dev)
m = re.search(r"PBKDF:\s*(\S+)\s*", out)
if not m or len(m.groups()) != 1:
self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
self.assertEqual(m.group(1), "pbkdf2")
# different options for argon2 -- all parameters set
pbkdf = BlockDev.CryptoLUKSPBKDF(type="argon2id", max_memory_kb=100*1024, iterations=10, parallel_threads=1)
extra = BlockDev.CryptoLUKSExtra(pbkdf=pbkdf)
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, None, self.keyfile, 0,
BlockDev.CryptoLUKSVersion.LUKS2, extra)
self.assertTrue(succ)
_ret, out, err = run_command("cryptsetup luksDump %s" % self.loop_dev)
m = re.search(r"PBKDF:\s*(\S+)\s*", out)
if not m or len(m.groups()) != 1:
self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
self.assertEqual(m.group(1), "argon2id")
m = re.search(r"Memory:\s*(\d+)\s*", out)
if not m or len(m.groups()) != 1:
self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
# both iterations and memory is set --> cryptsetup will use exactly max_memory_kb
self.assertEqual(int(m.group(1)), 100*1024)
m = re.search(r"Threads:\s*(\d+)\s*", out)
if not m or len(m.groups()) != 1:
self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
self.assertEqual(int(m.group(1)), 1)
m = re.search(r"Time cost:\s*(\d+)\s*", out)
if not m or len(m.groups()) != 1:
self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
self.assertEqual(int(m.group(1)), 10)
# different options for argon2 -- only memory set
pbkdf = BlockDev.CryptoLUKSPBKDF(max_memory_kb=100*1024)
extra = BlockDev.CryptoLUKSExtra(pbkdf=pbkdf)
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, None, self.keyfile, 0,
BlockDev.CryptoLUKSVersion.LUKS2, extra)
self.assertTrue(succ)
_ret, out, err = run_command("cryptsetup luksDump %s" % self.loop_dev)
m = re.search(r"Memory:\s*(\d+)\s*", out)
if not m or len(m.groups()) != 1:
self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
# only memory is set -> cryptsetup will run a benchmark and use
# at most max_memory_kb
self.assertLessEqual(int(m.group(1)), 100*1024)
# different options for argon2 -- only miterations set
pbkdf = BlockDev.CryptoLUKSPBKDF(iterations=5)
extra = BlockDev.CryptoLUKSExtra(pbkdf=pbkdf)
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-xts-plain64", 0, None, self.keyfile, 0,
BlockDev.CryptoLUKSVersion.LUKS2, extra)
self.assertTrue(succ)
_ret, out, err = run_command("cryptsetup luksDump %s" % self.loop_dev)
m = re.search(r"Time cost:\s*(\d+)\s*", out)
if not m or len(m.groups()) != 1:
self.fail("Failed to get pbkdf information from:\n%s %s" % (out, err))
self.assertEqual(int(m.group(1)), 5)
class CryptoTestResize(CryptoTestCase):
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_resize(self):
"""Verify that resizing LUKS device works"""
# the simple case with password
succ = self._luks_format(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
# resize to 512 KiB (1024 * 512B sectors)
succ = BlockDev.crypto_luks_resize("libblockdevTestLUKS", 1024)
self.assertTrue(succ)
# resize back to full size
succ = BlockDev.crypto_luks_resize("libblockdevTestLUKS", 0)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_resize(self):
"""Verify that resizing LUKS 2 device works"""
# the simple case with password
succ = self._luks2_format(self.loop_dev, PASSWD, self.keyfile)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
# resize without passphrase should fail
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_resize("libblockdevTestLUKS", 1024)
# resize to 512 KiB (1024 * 512B sectors)
succ = BlockDev.crypto_luks_resize("libblockdevTestLUKS", 1024, PASSWD)
self.assertTrue(succ)
# resize back to full size (using the keyfile)
succ = BlockDev.crypto_luks_resize("libblockdevTestLUKS", 0, None, self.keyfile)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
class CryptoTestOpenClose(CryptoTestCase):
def _luks_open_close(self, create_fn):
"""Verify that opening/closing LUKS device works"""
succ = create_fn(self.loop_dev, PASSWD, self.keyfile)
self.assertTrue(succ)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open("/non/existing/device", "libblockdevTestLUKS", PASSWD, None, False)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", None, None, False)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", "wrong-passhprase", None, False)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", None, "wrong-keyfile", False)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
# use the full /dev/mapper/ path
succ = BlockDev.crypto_luks_close("/dev/mapper/libblockdevTestLUKS")
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", None, self.keyfile, False)
self.assertTrue(succ)
# use just the LUKS device name
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_open_close(self):
self._luks_open_close(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_open_close(self):
self._luks_open_close(self._luks2_format)
class CryptoTestAddKey(CryptoTestCase):
def _add_key(self, create_fn):
"""Verify that adding key to LUKS device works"""
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_add_key(self.loop_dev, "wrong-passphrase", None, PASSWD2, None)
succ = BlockDev.crypto_luks_add_key(self.loop_dev, PASSWD, None, PASSWD2, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_add_key_blob(self.loop_dev, [ord(c) for c in PASSWD2], [ord(c) for c in PASSWD3])
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_add_key(self):
self._add_key(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_add_key(self):
self._add_key(self._luks2_format)
class CryptoTestRemoveKey(CryptoTestCase):
def _remove_key(self, create_fn):
"""Verify that removing key from LUKS device works"""
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_add_key(self.loop_dev, PASSWD, None, PASSWD2, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_add_key(self.loop_dev, PASSWD, None, PASSWD3, None)
self.assertTrue(succ)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_remove_key(self.loop_dev, "wrong-passphrase", None)
succ = BlockDev.crypto_luks_remove_key(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_remove_key_blob(self.loop_dev, [ord(c) for c in PASSWD2])
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_remove_key(self):
self._remove_key(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_remove_key(self):
self._remove_key(self._luks2_format)
class CryptoTestErrorLocale(CryptoTestCase):
def setUp(self):
self._orig_loc = None
CryptoTestCase.setUp(self)
self._orig_loc = ".".join(locale.getdefaultlocale())
def _clean_up(self):
CryptoTestCase._clean_up(self)
if self._orig_loc:
locale.setlocale(locale.LC_ALL, self._orig_loc)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@requires_locales({"cs_CZ.UTF-8"})
def test_error_locale_key(self):
"""Verify that the error msg is locale agnostic"""
succ = BlockDev.crypto_luks_format(self.loop_dev, None, 0, PASSWD, None, 0)
self.assertTrue(succ)
locale.setlocale(locale.LC_ALL, "cs_CZ.UTF-8")
try:
BlockDev.crypto_luks_remove_key(self.loop_dev, "wrong-passphrase", None)
except GLib.GError as e:
self.assertIn("Operation not permitted", str(e))
class CryptoTestChangeKey(CryptoTestCase):
def _change_key(self, create_fn):
"""Verify that changing key in LUKS device works"""
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
with six.assertRaisesRegex(self, GLib.GError, r"No keyslot with given passphrase found."):
BlockDev.crypto_luks_change_key(self.loop_dev, "wrong-passphrase", PASSWD2)
succ = BlockDev.crypto_luks_change_key(self.loop_dev, PASSWD, PASSWD2)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_change_key_blob(self.loop_dev, [ord(c) for c in PASSWD2], [ord(c) for c in PASSWD3])
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_change_key(self):
self._change_key(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_change_key(self):
self._change_key(self._luks2_format)
class CryptoTestIsLuks(CryptoTestCase):
def _is_luks(self, create_fn):
"""Verify that LUKS device recognition works"""
with self.assertRaises(GLib.GError):
BlockDev.crypto_device_is_luks("/non/existing/device")
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
is_luks = BlockDev.crypto_device_is_luks(self.loop_dev)
self.assertTrue(is_luks)
is_luks = BlockDev.crypto_device_is_luks(self.loop_dev2)
self.assertFalse(is_luks)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_is_luks(self):
self._is_luks(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_is_luks2(self):
self._is_luks(self._luks2_format)
class CryptoTestLuksStatus(CryptoTestCase):
def _luks_status(self, create_fn):
"""Verify that LUKS device status reporting works"""
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_status("/non/existing/device")
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
# use the full /dev/mapper path
status = BlockDev.crypto_luks_status("/dev/mapper/libblockdevTestLUKS")
self.assertEqual(status, "active")
# use just the LUKS device name
status = BlockDev.crypto_luks_status("libblockdevTestLUKS")
self.assertEqual(status, "active")
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_status("libblockdevTestLUKS")
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_status(self):
self._luks_status(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_status(self):
self._luks_status(self._luks2_format)
class CryptoTestGetUUID(CryptoTestCase):
def _get_uuid(self, create_fn):
"""Verify that getting LUKS device UUID works"""
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
uuid = BlockDev.crypto_luks_uuid(self.loop_dev)
self.assertTrue(uuid)
with self.assertRaises(GLib.GError):
uuid = BlockDev.crypto_luks_uuid(self.loop_dev2)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_get_uuid(self):
self._get_uuid(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_get_uuid(self):
self._get_uuid(self._luks2_format)
class CryptoTestGetMetadataSize(CryptoTestCase):
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_get_metadata_size(self):
"""Verify that getting LUKS 2 device metadata size works"""
succ = self._luks2_format(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
meta_size = BlockDev.crypto_luks_get_metadata_size(self.loop_dev)
ret, out, err = run_command("cryptsetup luksDump %s" % self.loop_dev)
if ret != 0:
self.fail("Failed to get LUKS 2 header from %s:\n%s %s" % (self.loop_dev, out, err))
m = re.search(r"offset:\s*([0-9]+)\s*\[bytes\]", out)
if m is None:
self.fail("Failed to get LUKS 2 offset information from %s:\n%s %s" % (self.loop_dev, out, err))
offset = int(m.group(1))
self.assertEquals(meta_size, offset, "LUKS 2 metadata sizes differ")
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_get_metadata_size(self):
"""Verify that getting LUKS device metadata size works"""
succ = self._luks_format(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
meta_size = BlockDev.crypto_luks_get_metadata_size(self.loop_dev)
ret, out, err = run_command("cryptsetup luksDump %s" % self.loop_dev)
if ret != 0:
self.fail("Failed to get LUKS header from %s:\n%s %s" % (self.loop_dev, out, err))
m = re.search(r"Payload offset:\s*([0-9]+)", out)
if m is None:
self.fail("Failed to get LUKS 2 offset information from %s:\n%s %s" % (self.loop_dev, out, err))
# offset value is in 512B blocks; we need to multiply to get the real metadata size
offset = int(m.group(1)) * 512
self.assertEquals(meta_size, offset, "LUKS metadata sizes differ")
class CryptoTestLuksOpenRW(CryptoTestCase):
def _luks_open_rw(self, create_fn):
"""Verify that a LUKS device can be activated as RW as well as RO"""
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
# tests that we can write something to the raw LUKS device
succ = BlockDev.utils_exec_and_report_error(["dd", "if=/dev/zero", "of=/dev/mapper/libblockdevTestLUKS", "bs=1M", "count=1"])
self.assertTrue(succ)
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
# now try the same with LUKS device opened as RO
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, True)
self.assertTrue(succ)
# tests that we can write something to the raw LUKS device
with self.assertRaises(GLib.GError):
BlockDev.utils_exec_and_report_error(["dd", "if=/dev/zero", "of=/dev/mapper/libblockdevTestLUKS", "bs=1M", "count=1"])
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_open_rw(self):
self._luks_open_rw(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_open_rw(self):
self._luks_open_rw(self._luks2_format)
class CryptoTestEscrow(CryptoTestCase):
def setUp(self):
super(CryptoTestEscrow, self).setUp()
# Create the certificate used to encrypt the escrow packet and backup passphrase.
# volume_key requires a nss database directory to decrypt any of the
# packet files, and python-nss is python2 only, just do everything with
# shell commands.
self.nss_dir = tempfile.mkdtemp(prefix='libblockdev_test_escrow')
self.addCleanup(shutil.rmtree, self.nss_dir)
subprocess.check_call(['certutil', '-d', self.nss_dir, '--empty-password', '-N'])
# Gather some entropy to keep certutil from asking for input
with tempfile.NamedTemporaryFile() as noise_file:
noise_file.write(os.urandom(20))
noise_file.flush()
subprocess.check_call(['certutil', '-d', self.nss_dir, '-S', '-x', '-n',
'escrow_cert', '-s', 'CN=Escrow Test', '-t', ',,TC', '-z',
noise_file.name])
# Export the public certificate
handle, self.public_cert = tempfile.mkstemp(prefix='libblockdev_test_escrow')
os.close(handle)
subprocess.check_call(['certutil', '-d', self.nss_dir, '-L', '-n', 'escrow_cert',
'-a', '-o', self.public_cert])
self.addCleanup(os.unlink, self.public_cert)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@skip_on(("centos", "enterprise_linux"), "7", reason="volume_key asks for password in non-interactive mode on this release")
@skip_on("debian", reason="volume_key asks for password in non-interactive mode on this release")
def test_escrow_packet(self):
"""Verify that an escrow packet can be created for a device"""
succ = BlockDev.crypto_luks_format(self.loop_dev, None, 0, PASSWD, None, 0)
self.assertTrue(succ)
escrow_dir = tempfile.mkdtemp(prefix='libblockdev_test_escrow')
self.addCleanup(shutil.rmtree, escrow_dir)
with open(self.public_cert, 'rb') as cert_file:
succ = BlockDev.crypto_escrow_device(self.loop_dev, PASSWD, cert_file.read(),
escrow_dir, None)
self.assertTrue(succ)
# Find the escrow packet
escrow_packet_file = '%s/%s-escrow' % (escrow_dir, BlockDev.crypto_luks_uuid(self.loop_dev))
self.assertTrue(os.path.isfile(escrow_packet_file))
# Use the volume_key utility (see note in setUp about why not python)
# to decrypt the escrow packet and restore access to the volume under
# a new passphrase
# Just use the existing temp directory to output the re-encrypted packet
# PASSWD2 is the passphrase of the new escrow packet
p = subprocess.Popen(['volume_key', '--reencrypt', '-b', '-d', self.nss_dir,
escrow_packet_file, '-o', '%s/escrow-out' % escrow_dir],
stdin=subprocess.PIPE)
p.communicate(input=('%s\0%s\0' % (PASSWD2, PASSWD2)).encode('utf-8'))
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, 'volume_key'
)
# Restore access to the volume
# PASSWD3 is the new passphrase for the LUKS device
p = subprocess.Popen(['volume_key', '--restore', '-b', self.loop_dev,
'%s/escrow-out' % escrow_dir], stdin=subprocess.PIPE)
p.communicate(input=('%s\0%s\0%s\0' % (PASSWD2, PASSWD3, PASSWD3)).encode('utf-8'))
if p.returncode != 0:
raise subprocess.CalledProcessError(p.returncode, 'volume_key')
# Open the volume with the new passphrase
succ = BlockDev.crypto_luks_open(self.loop_dev, 'libblockdevTestLUKS', PASSWD3, None)
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_backup_passphrase(self):
"""Verify that a backup passphrase can be created for a device"""
succ = BlockDev.crypto_luks_format(self.loop_dev, None, 0, PASSWD, None, 0)
self.assertTrue(succ)
escrow_dir = tempfile.mkdtemp(prefix='libblockdev_test_escrow')
self.addCleanup(shutil.rmtree, escrow_dir)
backup_passphrase = BlockDev.crypto_generate_backup_passphrase()
with open(self.public_cert, 'rb') as cert_file:
succ = BlockDev.crypto_escrow_device(self.loop_dev, PASSWD, cert_file.read(),
escrow_dir, backup_passphrase)
self.assertTrue(succ)
# Find the backup passphrase
escrow_backup_passphrase = "%s/%s-escrow-backup-passphrase" % (escrow_dir, BlockDev.crypto_luks_uuid(self.loop_dev))
self.assertTrue(os.path.isfile(escrow_backup_passphrase))
# Check that the encrypted file contains what we put in
env = {k: v for k, v in os.environ.items()}
env.update({"LC_ALL": "C"})
passphrase = subprocess.check_output(
['volume_key', '--secrets', '-d', self.nss_dir, escrow_backup_passphrase],
env=env)
passphrase = passphrase.strip().split()[1].decode('ascii')
self.assertEqual(passphrase, backup_passphrase)
# Check that the backup passphrase works
succ = BlockDev.crypto_luks_open(self.loop_dev, 'libblockdevTestLUKS', backup_passphrase, None)
self.assertTrue(succ)
class CryptoTestSuspendResume(CryptoTestCase):
def _luks_suspend_resume(self, create_fn):
succ = create_fn(self.loop_dev, PASSWD, self.keyfile)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None)
self.assertTrue(succ)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_suspend("/non/existing/device")
# use the full /dev/mapper/ path
succ = BlockDev.crypto_luks_suspend("/dev/mapper/libblockdevTestLUKS")
self.assertTrue(succ)
_ret, state, _err = run_command("lsblk -oSTATE -n /dev/mapper/libblockdevTestLUKS")
self.assertEqual(state, "suspended")
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_resume("libblockdevTestLUKS", None, None)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_resume("libblockdevTestLUKS", "wrong-passhprase", None)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_resume("libblockdevTestLUKS", None, "wrong-keyfile")
succ = BlockDev.crypto_luks_resume("libblockdevTestLUKS", PASSWD, None)
self.assertTrue(succ)
_ret, state, _err = run_command("lsblk -oSTATE -n /dev/mapper/libblockdevTestLUKS")
self.assertEqual(state, "running")
# use just the LUKS device name
succ = BlockDev.crypto_luks_suspend("libblockdevTestLUKS")
self.assertTrue(succ)
_ret, state, _err = run_command("lsblk -oSTATE -n /dev/mapper/libblockdevTestLUKS")
self.assertEqual(state, "suspended")
succ = BlockDev.crypto_luks_resume("libblockdevTestLUKS", None, self.keyfile)
self.assertTrue(succ)
_ret, state, _err = run_command("lsblk -oSTATE -n /dev/mapper/libblockdevTestLUKS")
self.assertEqual(state, "running")
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_suspend_resume(self):
"""Verify that suspending/resuming LUKS device works"""
self._luks_suspend_resume(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_suspend_resume(self):
"""Verify that suspending/resuming LUKS 2 device works"""
self._luks_suspend_resume(self._luks2_format)
class CryptoTestKillSlot(CryptoTestCase):
def _luks_kill_slot(self, create_fn):
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_add_key(self.loop_dev, PASSWD, None, PASSWD2, None)
self.assertTrue(succ)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_kill_slot("/non/existing/device", -1)
# invalid slot
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_kill_slot(self.loop_dev, -1)
# unused slot
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_kill_slot(self.loop_dev, 2)
# destroy second keyslot
succ = BlockDev.crypto_luks_kill_slot(self.loop_dev, 1)
self.assertTrue(succ)
# opening with the second passphrase should fail
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD2)
# opening with passphrase should still work
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_kill_slot(self):
"""Verify that killing a key slot on LUKS device works"""
self._luks_kill_slot(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_kill_slot(self):
"""Verify that killing a key slot on LUKS 2 device works"""
self._luks_kill_slot(self._luks2_format)
class CryptoTestHeaderBackupRestore(CryptoTestCase):
def setUp(self):
super(CryptoTestHeaderBackupRestore, self).setUp()
self.backup_dir = tempfile.mkdtemp(prefix='libblockdev_test_header')
self.addCleanup(shutil.rmtree, self.backup_dir)
def _luks_header_backup_restore(self, create_fn):
succ = create_fn(self.loop_dev, PASSWD, None)
self.assertTrue(succ)
backup_file = os.path.join(self.backup_dir, "luks-header.txt")
succ = BlockDev.crypto_luks_header_backup(self.loop_dev, backup_file)
self.assertTrue(succ)
self.assertTrue(os.path.isfile(backup_file))
# now completely destroy the luks header
ret, out, err = run_command("cryptsetup erase %s -q && wipefs -a %s" % (self.loop_dev, self.loop_dev))
if ret != 0:
self.fail("Failed to erase LUKS header from %s:\n%s %s" % (self.loop_dev, out, err))
_ret, fstype, _err = run_command("blkid -p -ovalue -sTYPE %s" % self.loop_dev)
self.assertFalse(fstype) # false == empty
# header is destroyed, should not be possible to open
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None)
# and restore the header back
succ = BlockDev.crypto_luks_header_restore(self.loop_dev, backup_file)
self.assertTrue(succ)
_ret, fstype, _err = run_command("blkid -p -ovalue -sTYPE %s" % self.loop_dev)
self.assertEqual(fstype, "crypto_LUKS")
# opening should now work
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_header_backup_restore(self):
"""Verify that header backup/restore with LUKS works"""
self._luks_header_backup_restore(self._luks_format)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_header_backup_restore(self):
"""Verify that header backup/restore with LUKS2 works"""
self._luks_header_backup_restore(self._luks2_format)
class CryptoTestInfo(CryptoTestCase):
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
def test_luks_format(self):
"""Verify that we can get information about a LUKS device"""
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-cbc-essiv:sha256", 256, PASSWD, None, 0)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
info = BlockDev.crypto_luks_info("libblockdevTestLUKS")
self.assertIsNotNone(info)
self.assertEqual(info.version, BlockDev.CryptoLUKSVersion.LUKS1)
self.assertEqual(info.cipher, "aes")
self.assertEqual(info.mode, "cbc-essiv:sha256")
self.assertEqual(info.backing_device, self.loop_dev)
_ret, uuid, _err = run_command("blkid -p -ovalue -sUUID %s" % self.loop_dev)
self.assertEqual(info.uuid, uuid)
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_format(self):
"""Verify that we can get information about a LUKS 2 device"""
extra = BlockDev.CryptoLUKSExtra()
extra.sector_size = 4096
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-cbc-essiv:sha256", 256, PASSWD, None, 0,
BlockDev.CryptoLUKSVersion.LUKS2, extra)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
info = BlockDev.crypto_luks_info("libblockdevTestLUKS")
self.assertIsNotNone(info)
self.assertEqual(info.version, BlockDev.CryptoLUKSVersion.LUKS2)
self.assertEqual(info.cipher, "aes")
self.assertEqual(info.mode, "cbc-essiv:sha256")
self.assertEqual(info.backing_device, self.loop_dev)
self.assertEqual(info.sector_size, 4096)
_ret, uuid, _err = run_command("blkid -p -ovalue -sUUID %s" % self.loop_dev)
self.assertEqual(info.uuid, uuid)
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
class CryptoTestIntegrity(CryptoTestCase):
@unittest.skipIf("SKIP_SLOW" in os.environ, "skipping slow tests")
@unittest.skipUnless(HAVE_LUKS2, "LUKS 2 not supported")
def test_luks2_integrity(self):
"""Verify that we can get create a LUKS 2 device with integrity"""
extra = BlockDev.CryptoLUKSExtra()
extra.integrity = "hmac(sha256)"
succ = BlockDev.crypto_luks_format(self.loop_dev, "aes-cbc-essiv:sha256", 512, PASSWD, None, 0,
BlockDev.CryptoLUKSVersion.LUKS2, extra)
self.assertTrue(succ)
succ = BlockDev.crypto_luks_open(self.loop_dev, "libblockdevTestLUKS", PASSWD, None, False)
self.assertTrue(succ)
info = BlockDev.crypto_integrity_info("libblockdevTestLUKS")
self.assertIsNotNone(info)
self.assertEqual(info.algorithm, "hmac(sha256)")
# get integrity device dm name
_ret, int_name, _err = run_command('ls /sys/block/%s/holders/' % self.loop_dev.split("/")[-1])
self.assertTrue(int_name) # true == not empty
tag_size = read_file("/sys/block/%s/integrity/tag_size" % int_name)
self.assertEqual(info.tag_size, int(tag_size))
succ = BlockDev.crypto_luks_close("libblockdevTestLUKS")
self.assertTrue(succ)
class CryptoTestTrueCrypt(CryptoTestCase):
# we can't create TrueCrypt/VeraCrypt formats using libblockdev
# so we are using these images from cryptsetup test suite
# https://gitlab.com/cryptsetup/cryptsetup/blob/master/tests/tcrypt-images.tar.bz2
tc_img = "tc-sha512-xts-aes"
vc_img = "vc-sha512-xts-aes"
passphrase = "aaaaaaaaaaaa"
tempdir = None
@classmethod
def setUpClass(cls):
super(CryptoTestTrueCrypt, cls).setUpClass()
cls.tempdir = tempfile.mkdtemp(prefix="bd_test_tcrypt")
images = os.path.join(os.path.dirname(__file__), "truecrypt-images.tar.gz")
with tarfile.open(images, "r") as tar:
tar.extractall(cls.tempdir)
@classmethod
def tearDownClass(cls):
super(CryptoTestTrueCrypt, cls).tearDownClass()
shutil.rmtree(cls.tempdir)
def setUp(self):
self.addCleanup(self._clean_up)
succ, loop = BlockDev.loop_setup(os.path.join(self.tempdir, self.tc_img))
if not succ:
raise RuntimeError("Failed to setup loop device for testing")
self.tc_dev = "/dev/%s" % loop
succ, loop = BlockDev.loop_setup(os.path.join(self.tempdir, self.vc_img))
if not succ:
raise RuntimeError("Failed to setup loop device for testing")
self.vc_dev = "/dev/%s" % loop
def _clean_up(self):
try:
BlockDev.crypto_tc_close("libblockdevTestTC")
except:
pass
succ = BlockDev.loop_teardown(self.tc_dev)
if not succ:
raise RuntimeError("Failed to tear down loop device used for testing")
succ = BlockDev.loop_teardown(self.vc_dev)
if not succ:
raise RuntimeError("Failed to tear down loop device used for testing")
def test_truecrypt_open_close(self):
"""Verify that opening/closing TrueCrypt device works"""
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open("/non/existing/device", "libblockdevTestTC", self.passphrase)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.tc_dev, "libblockdevTestTC", None)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.tc_dev, "libblockdevTestTC", "wrong-passhprase")
succ = BlockDev.crypto_tc_open(self.tc_dev, "libblockdevTestTC", self.passphrase)
self.assertTrue(succ)
self.assertTrue(os.path.exists("/dev/mapper/libblockdevTestTC"))
succ = BlockDev.crypto_tc_close("libblockdevTestTC")
self.assertTrue(succ)
self.assertFalse(os.path.exists("/dev/mapper/libblockdevTestTC"))
def test_veracrypt_open_close(self):
"""Verify that opening/closing VeraCrypt device works"""
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open("/non/existing/device", "libblockdevTestTC", self.passphrase)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.vc_dev, "libblockdevTestTC", None)
with self.assertRaises(GLib.GError):
BlockDev.crypto_luks_open(self.vc_dev, "libblockdevTestTC", "wrong-passhprase")
succ = BlockDev.crypto_tc_open(self.vc_dev, "libblockdevTestTC", self.passphrase, veracrypt=True)
self.assertTrue(succ)
self.assertTrue(os.path.exists("/dev/mapper/libblockdevTestTC"))
succ = BlockDev.crypto_tc_close("libblockdevTestTC")
self.assertTrue(succ)
self.assertFalse(os.path.exists("/dev/mapper/libblockdevTestTC"))
|