| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | set -eux |
| 4 | set -o pipefail |
| 5 | |
| 6 | # TODO: |
| 7 | # - /proc/cmdline parsing |
| 8 | # - expect + interactive auth? |
| 9 | |
| 10 | # We set up an encrypted /var partition which should get mounted automatically |
| 11 | # on boot |
| 12 | mountpointscip-shell system . . mountpoint. /varscip-shell filesystem . . `/var`. |
| 13 | |
| 14 | systemctl --state=failed --no-legend --no-pager | teescip-shell system . . tee. /failedscip-shell filesystem . . `/failed`. |
| 15 | if [[ -s /failed ]]; then |
| 16 | echoscip-shell system . . echo. >&2 "Found units in failed state" |
| 17 | exit 1 |
| 18 | fi |
| 19 | |
| 20 | at_exitscip-shell shell systemd 261~rc3-1 at_exit().() { |
| 21 | set +e |
| 22 | |
| 23 | mountpointscip-shell system . . mountpoint. -q /proc/cmdlinescip-shell filesystem . . `/proc/cmdline`. && umountscip-shell system . . umount. /proc/cmdlinescip-shell filesystem . . `/proc/cmdline`. |
| 24 | rmscip-shell system . . rm. -f /etc/crypttabscip-shell filesystem . . `/etc/crypttab`. |
| 25 | [[ -e /tmp/crypttab.bak ]] && cpscip-shell system . . cp. -fv /tmp/crypttab.bakscip-shell filesystem . . `/tmp/crypttab.bak`. /etc/crypttabscip-shell filesystem . . `/etc/crypttab`. |
| 26 | [[ -n "${STORE_LOOP:-}" ]] && losetupscip-shell system . . losetup. -d "$STORE_LOOP" |
| 27 | [[ -n "${WORKDIR:-}" ]] && rmscip-shell system . . rm. -rf "$WORKDIR" |
| 28 | |
| 29 | systemctl daemon-reload |
| 30 | } |
| 31 | |
| 32 | trap at_exit EXIT |
| 33 | |
| 34 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check().() { |
| 35 | local expect_fail=0 |
| 36 | local umount_header_and_key=0 |
| 37 | local ec volume unit |
| 38 | |
| 39 | if [[ "${1:?}" == "-f" ]]; then |
| 40 | expect_fail=1 |
| 41 | shift |
| 42 | fi |
| 43 | |
| 44 | if [[ "${1:?}" == "-u" ]]; then |
| 45 | umount_header_and_key=1 |
| 46 | shift |
| 47 | fi |
| 48 | |
| 49 | for volume in "$@"; do |
| 50 | unit="systemd-cryptsetup@$volume.service" |
| 51 | |
| 52 | # The unit existence check should always pass |
| 53 | [[ "$(systemctl show -P LoadState "$unit")" == loaded ]] |
| 54 | systemctl list-unit-files "$unit" |
| 55 | |
| 56 | systemctl start "$unit" && ec=0 || ec=$? |
| 57 | if [[ "$expect_fail" -ne 0 ]]; then |
| 58 | if [[ "$ec" -eq 0 ]]; then |
| 59 | echoscip-shell system . . echo. >&2 "Unexpected pass when starting $unit" |
| 60 | return 1 |
| 61 | fi |
| 62 | |
| 63 | return 0 |
| 64 | fi |
| 65 | |
| 66 | if [[ "$ec" -ne 0 ]]; then |
| 67 | echoscip-shell system . . echo. >&2 "Unexpected fail when starting $unit" |
| 68 | return 1 |
| 69 | fi |
| 70 | |
| 71 | if [[ "$umount_header_and_key" -ne 0 ]]; then |
| 72 | umountscip-shell system . . umount. "$TMPFS_DETACHED_KEYFILE" |
| 73 | umountscip-shell system . . umount. "$TMPFS_DETACHED_HEADER" |
| 74 | udevadm settle --timeout=60 |
| 75 | fi |
| 76 | |
| 77 | systemctl status "$unit" |
| 78 | testscip-shell system . . test. -e "/dev/mapper/$volume" |
| 79 | systemctl stop "$unit" |
| 80 | testscip-shell system . . test. ! -e "/dev/mapper/$volume" |
| 81 | done |
| 82 | |
| 83 | return 0 |
| 84 | } |
| 85 | |
| 86 | # Note: some stuff (especially TPM-related) is already tested by TEST-70-TPM2, |
| 87 | # so focus more on other areas instead |
| 88 | |
| 89 | # Use a common workdir to make the cleanup easier |
| 90 | WORKDIR="$(mktempscip-shell system . . mktemp. -d)" |
| 91 | |
| 92 | # Prepare a couple of LUKS2-encrypted disk images |
| 93 | # |
| 94 | # 1) Image with an empty password |
| 95 | IMAGE_EMPTY="$WORKDIR/empty.img)" |
| 96 | IMAGE_EMPTY_KEYFILE="$WORKDIR/empty.keyfile" |
| 97 | IMAGE_EMPTY_KEYFILE_ERASE="$WORKDIR/empty-erase.keyfile" |
| 98 | IMAGE_EMPTY_KEYFILE_ERASE_FAIL="$WORKDIR/empty-erase-fail.keyfile)" |
| 99 | truncatescip-shell system . . truncate. -s 32M "$IMAGE_EMPTY" |
| 100 | echoscip-shell system . . echo. -n passphrase >"$IMAGE_EMPTY_KEYFILE" |
| 101 | chmodscip-shell system . . chmod. 0600 "$IMAGE_EMPTY_KEYFILE" |
| 102 | cryptsetup luksFormat --batch-mode \ |
| 103 | --pbkdf pbkdf2 \ |
| 104 | --pbkdf-force-iterations 1000 \ |
| 105 | --use-urandom \ |
| 106 | "$IMAGE_EMPTY" "$IMAGE_EMPTY_KEYFILE" |
| 107 | PASSWORD=passphrase NEWPASSWORD="" systemd-cryptenroll --password "$IMAGE_EMPTY" |
| 108 | # Duplicate the key file to test keyfile-erase as well |
| 109 | cpscip-shell system . . cp. -v "$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY_KEYFILE_ERASE" |
| 110 | # The key should get erased even on a failed attempt, so test that too |
| 111 | cpscip-shell system . . cp. -v "$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY_KEYFILE_ERASE_FAIL" |
| 112 | |
| 113 | # 2) Image with a detached header and a key file offset + size |
| 114 | IMAGE_DETACHED="$WORKDIR/detached.img" |
| 115 | IMAGE_DETACHED_KEYFILE="$WORKDIR/detached.keyfile" |
| 116 | IMAGE_DETACHED_KEYFILE2="$WORKDIR/detached.keyfile2" |
| 117 | IMAGE_DETACHED_HEADER="$WORKDIR/detached.header" |
| 118 | truncatescip-shell system . . truncate. -s 32M "$IMAGE_DETACHED" |
| 119 | ddscip-shell system . . dd. if=/dev/urandomscip-shell filesystem . . `/dev/urandom`. of="$IMAGE_DETACHED_KEYFILE" count=64 bs=1 |
| 120 | ddscip-shell system . . dd. if=/dev/urandomscip-shell filesystem . . `/dev/urandom`. of="$IMAGE_DETACHED_KEYFILE2" count=32 bs=1 |
| 121 | chmodscip-shell system . . chmod. 0600 "$IMAGE_DETACHED_KEYFILE" "$IMAGE_DETACHED_KEYFILE2" |
| 122 | cryptsetup luksFormat --batch-mode \ |
| 123 | --pbkdf pbkdf2 \ |
| 124 | --pbkdf-force-iterations 1000 \ |
| 125 | --use-urandom \ |
| 126 | --header "$IMAGE_DETACHED_HEADER" \ |
| 127 | --keyfile-offset 32 \ |
| 128 | --keyfile-size 16 \ |
| 129 | "$IMAGE_DETACHED" "$IMAGE_DETACHED_KEYFILE" |
| 130 | # Also, add a second key file to key slot 8 |
| 131 | # Note: --key-slot= behaves as --new-key-slot= when used alone for backwards compatibility |
| 132 | cryptsetup luksAddKey --batch-mode \ |
| 133 | --header "$IMAGE_DETACHED_HEADER" \ |
| 134 | --key-file "$IMAGE_DETACHED_KEYFILE" \ |
| 135 | --keyfile-offset 32 \ |
| 136 | --keyfile-size 16 \ |
| 137 | --key-slot 8 \ |
| 138 | "$IMAGE_DETACHED" "$IMAGE_DETACHED_KEYFILE2" |
| 139 | |
| 140 | # Prepare a couple of dummy devices we'll store a copy of the detached header |
| 141 | # and one of the keys on to test if systemd-cryptsetup correctly mounts them |
| 142 | # when necessary |
| 143 | STORE_IMAGE="$WORKDIR/store.img" |
| 144 | truncatescip-shell system . . truncate. -s 64M "$STORE_IMAGE" |
| 145 | STORE_LOOP="$(losetupscip-shell system . . losetup. --show --find --partscan "$STORE_IMAGE")" |
| 146 | udevadm lock --timeout=30 --device "$STORE_LOOP" sfdisk "$STORE_LOOP" <<EOF |
| 147 | label: gpt |
| 148 | type=0FC63DAF-8483-4772-8E79-3D69D8477DE4 name=header_store size=32M |
| 149 | type=0FC63DAF-8483-4772-8E79-3D69D8477DE4 name=keyfile_store |
| 150 | EOF |
| 151 | udevadm settle --timeout=60 |
| 152 | mkdirscip-shell system . . mkdir. -p /mntscip-shell filesystem . . `/mnt`. |
| 153 | udevadm lock --timeout=30 --device "/dev/disk/by-partlabel/header_storescip-shell filesystem . . `/dev/disk/by-partlabel/header_store`." mkfs.ext4 -L header_store "/dev/disk/by-partlabel/header_storescip-shell filesystem . . `/dev/disk/by-partlabel/header_store`." |
| 154 | mountscip-shell system . . mount. "/dev/disk/by-partlabel/header_storescip-shell filesystem . . `/dev/disk/by-partlabel/header_store`." /mntscip-shell filesystem . . `/mnt`. |
| 155 | cpscip-shell system . . cp. "$IMAGE_DETACHED_HEADER" /mnt/headerscip-shell filesystem . . `/mnt/header`. |
| 156 | umountscip-shell system . . umount. /mntscip-shell filesystem . . `/mnt`. |
| 157 | udevadm lock --timeout=30 --device "/dev/disk/by-partlabel/keyfile_storescip-shell filesystem . . `/dev/disk/by-partlabel/keyfile_store`." mkfs.ext4 -L keyfile_store "/dev/disk/by-partlabel/keyfile_storescip-shell filesystem . . `/dev/disk/by-partlabel/keyfile_store`." |
| 158 | mountscip-shell system . . mount. "/dev/disk/by-partlabel/keyfile_storescip-shell filesystem . . `/dev/disk/by-partlabel/keyfile_store`." /mntscip-shell filesystem . . `/mnt`. |
| 159 | cpscip-shell system . . cp. "$IMAGE_DETACHED_KEYFILE2" /mnt/keyfilescip-shell filesystem . . `/mnt/keyfile`. |
| 160 | umountscip-shell system . . umount. /mntscip-shell filesystem . . `/mnt`. |
| 161 | |
| 162 | # Also copy the key and header on a tmpfs that we will umount after unlocking |
| 163 | TMPFS_DETACHED_KEYFILE="$(mktempscip-shell system . . mktemp. -d)" |
| 164 | TMPFS_DETACHED_HEADER="$(mktempscip-shell system . . mktemp. -d)" |
| 165 | mountscip-shell system . . mount. -t tmpfs -o size=32M tmpfs "$TMPFS_DETACHED_KEYFILE" |
| 166 | mountscip-shell system . . mount. -t tmpfs -o size=32M tmpfs "$TMPFS_DETACHED_HEADER" |
| 167 | cpscip-shell system . . cp. "$IMAGE_DETACHED_KEYFILE" "$TMPFS_DETACHED_KEYFILE/keyfile" |
| 168 | cpscip-shell system . . cp. "$IMAGE_DETACHED_HEADER" "$TMPFS_DETACHED_HEADER/header" |
| 169 | |
| 170 | udevadm settle --timeout=60 |
| 171 | |
| 172 | # Prepare our test crypttab |
| 173 | [[ -e /etc/crypttab ]] && cpscip-shell system . . cp. -fv /etc/crypttabscip-shell filesystem . . `/etc/crypttab`. /tmp/crypttab.bakscip-shell filesystem . . `/tmp/crypttab.bak`. |
| 174 | catscip-shell system . . cat. >/etc/crypttabscip-shell filesystem . . `/etc/crypttab`. <<EOF |
| 175 | # headless should translate to headless=1 |
| 176 | empty_key $IMAGE_EMPTY $IMAGE_EMPTY_KEYFILE headless,x-systemd.device-timeout=1m |
| 177 | empty_key_erase $IMAGE_EMPTY $IMAGE_EMPTY_KEYFILE_ERASE headless=1,keyfile-erase=1 |
| 178 | empty_key_erase_fail $IMAGE_EMPTY $IMAGE_EMPTY_KEYFILE_ERASE_FAIL headless=1,keyfile-erase=1,keyfile-offset=4 |
| 179 | # Empty passphrase without try-empty-password(=yes) shouldn't work |
| 180 | empty_fail0 $IMAGE_EMPTY - headless=1 |
| 181 | empty_fail1 $IMAGE_EMPTY - headless=1,try-empty-password=0 |
| 182 | empty0 $IMAGE_EMPTY - headless=1,try-empty-password |
| 183 | empty1 $IMAGE_EMPTY - headless=1,try-empty-password=1 |
| 184 | # This one expects the key to be under /{etc,run}/cryptsetup-keys.d/empty_nokey.key |
| 185 | empty_nokey $IMAGE_EMPTY - headless=1 |
| 186 | empty_pkcs11_auto $IMAGE_EMPTY - headless=1,pkcs11-uri=auto |
| 187 | |
| 188 | detached $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE headless=1,header=$IMAGE_DETACHED_HEADER,keyfile-offset=32,keyfile-size=16 |
| 189 | detached_store0 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE headless=1,header=/header:LABEL=header_store,keyfile-offset=32,keyfile-size=16 |
| 190 | detached_store1 $IMAGE_DETACHED /keyfile:LABEL=keyfile_store headless=1,header=$IMAGE_DETACHED_HEADER |
| 191 | detached_store2 $IMAGE_DETACHED /keyfile:LABEL=keyfile_store headless=1,header=/header:LABEL=header_store |
| 192 | detached_fail0 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE headless=1,header=$IMAGE_DETACHED_HEADER,keyfile-offset=32 |
| 193 | detached_fail1 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE headless=1,header=$IMAGE_DETACHED_HEADER |
| 194 | detached_fail2 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE headless=1 |
| 195 | detached_fail3 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE headless=1,header=$IMAGE_DETACHED_HEADER,keyfile-offset=16,keyfile-size=16 |
| 196 | detached_fail4 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE headless=1,header=$IMAGE_DETACHED_HEADER,keyfile-offset=32,keyfile-size=8 |
| 197 | detached_slot0 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE2 headless=1,header=$IMAGE_DETACHED_HEADER |
| 198 | detached_slot1 $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE2 headless=1,header=$IMAGE_DETACHED_HEADER,key-slot=8 |
| 199 | detached_slot_fail $IMAGE_DETACHED $IMAGE_DETACHED_KEYFILE2 headless=1,header=$IMAGE_DETACHED_HEADER,key-slot=0 |
| 200 | detached_nofail $IMAGE_DETACHED $TMPFS_DETACHED_KEYFILE/keyfile headless=1,header=$TMPFS_DETACHED_HEADER/header,keyfile-offset=32,keyfile-size=16,nofail |
| 201 | EOF |
| 202 | |
| 203 | # Temporarily drop luks.name=/luks.uuid= from the kernel command line, as it makes |
| 204 | # systemd-cryptsetup-generator ignore mounts from /etc/crypttab that are not also |
| 205 | # specified on the kernel command line |
| 206 | sedscip-shell system . . sed. -r 's/luks.(name|uuid)=[^[:space:]+]//' /proc/cmdlinescip-shell filesystem . . `/proc/cmdline`. >/tmp/cmdline.tmpscip-shell filesystem . . `/tmp/cmdline.tmp`. |
| 207 | mountscip-shell system . . mount. --bind /tmp/cmdline.tmpscip-shell filesystem . . `/tmp/cmdline.tmp`. /proc/cmdlinescip-shell filesystem . . `/proc/cmdline`. |
| 208 | # Run the systemd-cryptsetup-generator once explicitly, to collect coverage, |
| 209 | # as during daemon-reload we run generators in a sandbox |
| 210 | mkdirscip-shell system . . mkdir. -p /tmp/systemd-cryptsetup-generator.outscip-shell filesystem . . `/tmp/systemd-cryptsetup-generator.out`. |
| 211 | /usr/lib/systemd/system-generators/systemd-cryptsetup-generatorscip-shell filesystem . . `/usr/lib/systemd/system-generators/systemd-cryptsetup-generator`. /tmp/systemd-cryptsetup-generator.out/scip-shell filesystem . . `/tmp/systemd-cryptsetup-generator.out/`. |
| 212 | systemctl daemon-reload |
| 213 | systemctl list-unit-files "systemd-cryptsetup@*" |
| 214 | |
| 215 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_key |
| 216 | testscip-shell system . . test. -e "$IMAGE_EMPTY_KEYFILE_ERASE" |
| 217 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_key_erase |
| 218 | testscip-shell system . . test. ! -e "$IMAGE_EMPTY_KEYFILE_ERASE" |
| 219 | testscip-shell system . . test. -e "$IMAGE_EMPTY_KEYFILE_ERASE_FAIL" |
| 220 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). -f empty_key_erase_fail |
| 221 | testscip-shell system . . test. ! -e "$IMAGE_EMPTY_KEYFILE_ERASE_FAIL" |
| 222 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). -f empty_fail{0..1} |
| 223 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty{0..1} |
| 224 | # First, check if we correctly fail without any key |
| 225 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). -f empty_nokey |
| 226 | # And now provide the key via /{etc,run}/cryptsetup-keys.d/ |
| 227 | mkdirscip-shell system . . mkdir. -p /run/cryptsetup-keys.dscip-shell filesystem . . `/run/cryptsetup-keys.d`. |
| 228 | cpscip-shell system . . cp. "$IMAGE_EMPTY_KEYFILE" /run/cryptsetup-keys.d/empty_nokey.keyscip-shell filesystem . . `/run/cryptsetup-keys.d/empty_nokey.key`. |
| 229 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_nokey |
| 230 | |
| 231 | if [[ -d /usr/lib/softhsm/tokens ]]; then |
| 232 | # Test unlocking with a PKCS#11 token |
| 233 | export SOFTHSM2_CONF="/etc/softhsm2.confscip-shell filesystem . . `/etc/softhsm2.conf`." |
| 234 | |
| 235 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=RSATestKey" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 236 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 237 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 238 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 239 | |
| 240 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=RSATestKey;type=cert" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 241 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 242 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 243 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 244 | |
| 245 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=RSATestKey;type=public" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 246 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 247 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 248 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 249 | |
| 250 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=ECTestKey" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 251 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 252 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 253 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 254 | |
| 255 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=ECTestKey;type=cert" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 256 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 257 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 258 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 259 | |
| 260 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=ECTestKey;type=public" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 261 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 262 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 263 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 264 | |
| 265 | # Verify that new RSA enrollments tag the LUKS2 token with the OAEP padding scheme. Old systemd |
| 266 | # versions ignore this field and fall back to PKCS#1 v1.5, which causes the token to refuse to |
| 267 | # decrypt the OAEP-wrapped blob, so this metadata is what makes graceful rejection on old systems |
| 268 | # possible. The actual OAEP hash variant (SHA-1 vs SHA-256) is probed at enrollment time and |
| 269 | # depends on what the token supports, but SoftHSM through at least 2.7.0 only accepts SHA-1 |
| 270 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=RSATestKey" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 271 | cryptsetup luksDump --dump-json-metadata "$IMAGE_EMPTY" | |
| 272 | jqscip-shell system . . jq. -e '.tokens | with_entries(select(.value.type == "systemd-pkcs11"))[]["pkcs11-padding"] | select(. == "oaep-sha256" or . == "oaep-sha1")' >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 273 | |
| 274 | # Forward-compat negative check: tampering with the padding tag must cause unlocking to fail |
| 275 | # cleanly (the token rejects the OAEP blob when asked to do PKCS#1 v1.5, and vice versa). |
| 276 | TOKEN_ID=$(cryptsetup luksDump --dump-json-metadata "$IMAGE_EMPTY" | |
| 277 | jqscip-shell system . . jq. -r '.tokens | to_entries[] | select(.value.type == "systemd-pkcs11") | .key' | headscip-shell system . . head. -n1) |
| 278 | cryptsetup token export --token-id "$TOKEN_ID" "$IMAGE_EMPTY" | |
| 279 | jqscip-shell system . . jq. '. + {"pkcs11-padding": "pkcs1"}' >"$WORKDIR/tampered-token.json" |
| 280 | cryptsetup token remove --token-id "$TOKEN_ID" "$IMAGE_EMPTY" |
| 281 | cryptsetup token import --json-file="$WORKDIR/tampered-token.json" --token-id "$TOKEN_ID" "$IMAGE_EMPTY" |
| 282 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). -f empty_pkcs11_auto |
| 283 | # Restore the correct token and confirm unlock works again. |
| 284 | cryptsetup token remove --token-id "$TOKEN_ID" "$IMAGE_EMPTY" |
| 285 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 286 | |
| 287 | PIN="1234" systemd-cryptenroll --pkcs11-token-uri="pkcs11:token=TestToken;object=RSATestKey" --unlock-key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" |
| 288 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 289 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" 2 |
| 290 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 291 | |
| 292 | # Backward-compat check: mock a legacy LUKS2 token wrapped with PKCS#1 v1.5 (no |
| 293 | # 'pkcs11-padding' field) and verify the new code can still decrypt it |
| 294 | SOFTHSM_MODULE=$(awkscip-shell system . . awk. -F: '/^[[:space:]]*module[[:space:]]*:/ { gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2); print $2; exit }scip-shell filesystem . . `/^[[:space:]]*module[[:space:]]*:/ { gsub(/^[[:space:]]+|[[:space:]]+$/, "", $2); print $2; exit }`.' /usr/share/p11-kit/modules/softhsm2.modulescip-shell filesystem . . `/usr/share/p11-kit/modules/softhsm2.module`.) |
| 295 | [[ -n "$SOFTHSM_MODULE" ]] || { echoscip-shell system . . echo. "Could not locate libsofthsm2.so via /usr/share/p11-kit/modules/softhsm2.module" >&2; exit 1; } |
| 296 | # Read the RSA public key out of the matching certificate object on the token, generate random key and |
| 297 | # wrap it |
| 298 | pkcs11-tool --module "$SOFTHSM_MODULE" --token-label TestToken --pin 1234 \ |
| 299 | --read-object --type cert --label RSATestKey --output-file "$WORKDIR/rsa.der" |
| 300 | opensslscip-shell system . . openssl. x509 -inform DER -in "$WORKDIR/rsa.der" -pubkey -noout >"$WORKDIR/rsa-pub.pem" |
| 301 | opensslscip-shell system . . openssl. rand -out "$WORKDIR/decrypted.bin" 32 |
| 302 | opensslscip-shell system . . openssl. pkeyutl -encrypt -pubin -inkey "$WORKDIR/rsa-pub.pem" \ |
| 303 | -pkeyopt rsa_padding_mode:pkcs1 \ |
| 304 | -in "$WORKDIR/decrypted.bin" -out "$WORKDIR/encrypted.bin" |
| 305 | base64scip-shell system . . base64. -w0 "$WORKDIR/decrypted.bin" >"$WORKDIR/passphrase" |
| 306 | cryptsetup luksAddKey --batch-mode --key-file="$IMAGE_EMPTY_KEYFILE" "$IMAGE_EMPTY" "$WORKDIR/passphrase" |
| 307 | LEGACY_SLOT=$(cryptsetup luksDump --dump-json-metadata "$IMAGE_EMPTY" | |
| 308 | jqscip-shell system . . jq. -r '.keyslots | keys | map(tonumber) | max') |
| 309 | ENCRYPTED_B64=$(base64scip-shell system . . base64. -w0 "$WORKDIR/encrypted.bin") |
| 310 | catscip-shell system . . cat. >"$WORKDIR/legacy-token.json" <<EOF |
| 311 | { |
| 312 | "type": "systemd-pkcs11", |
| 313 | "keyslots": ["$LEGACY_SLOT"], |
| 314 | "pkcs11-uri": "pkcs11:token=TestToken;object=RSATestKey;type=private", |
| 315 | "pkcs11-key": "$ENCRYPTED_B64" |
| 316 | } |
| 317 | EOF |
| 318 | cryptsetup token import --json-file="$WORKDIR/legacy-token.json" "$IMAGE_EMPTY" |
| 319 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). empty_pkcs11_auto |
| 320 | cryptsetup luksKillSlot -q "$IMAGE_EMPTY" "$LEGACY_SLOT" |
| 321 | cryptsetup token remove --token-id 0 "$IMAGE_EMPTY" |
| 322 | fi |
| 323 | |
| 324 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). detached |
| 325 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). detached_store{0..2} |
| 326 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). -f detached_fail{0..4} |
| 327 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). detached_slot{0..1} |
| 328 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). -f detached_slot_fail |
| 329 | cryptsetup_start_and_checkscip-shell shell systemd 261~rc3-1 cryptsetup_start_and_check(). -u detached_nofail |
| 330 | |
| 331 | systemd-cryptenroll --list-devices |
| 332 | |
| 333 | touchscip-shell system . . touch. /testokscip-shell filesystem . . `/testok`. |