2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2019 Google LLC
7 # Test adding a key to a filesystem's fscrypt keyring via an
8 # "fscrypt-provisioning" keyring key. This is an alternative to the normal
9 # method where the raw key is given directly.
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
17 status=1 # failure is the default!
18 trap "_cleanup; exit \$status" 0 1 2 3 15
26 # get standard environment, filters and checks
31 # remove previous $seqres.full before test
34 # real QA test starts here
37 _require_scratch_encryption -v 2
38 _require_command "$KEYCTL_PROG" keyctl
41 _scratch_mkfs_encrypted &>> $seqres.full
43 _require_add_enckey_by_key_id $SCRATCH_MNT
45 test_with_policy_version()
48 local dir=$SCRATCH_MNT/dir
52 echo "# =========================="
53 echo "# Test with policy version $vers"
54 echo "# =========================="
58 local keytype=$FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR
59 local keyspec=$TEST_KEY_DESCRIPTOR
60 local add_enckey_args="-d $TEST_KEY_DESCRIPTOR"
63 local keytype=$FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
64 local keyspec=$TEST_KEY_IDENTIFIER
65 local add_enckey_args=""
68 _fail "Unknown policy version: $vers"
72 # First add the key in the regular way (raw key given directly), create
73 # an encrypted file with some contents, and remove the key. After this,
74 # the encrypted file should no longer be readable.
76 echo -e "\n# Adding key to filesystem"
77 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" $add_enckey_args
79 echo -e "\n# Creating encrypted file"
81 _set_encpolicy $dir $keyspec
82 echo "contents" > $dir/file
84 echo -e "\n# Removing key from filesystem"
85 _rm_enckey $SCRATCH_MNT $keyspec
86 cat $dir/file |& _filter_scratch
88 # Now we should be able to add the key back via an fscrypt-provisioning
89 # key which contains the raw key, instead of providing the raw key
90 # directly. After this, the encrypted file should be readable again.
92 echo -e "\n# Adding fscrypt-provisioning key"
93 keyid=$(_add_fscrypt_provisioning_key $keyspec $keytype "$TEST_RAW_KEY")
95 echo -e "\n# Adding key to filesystem via fscrypt-provisioning key"
96 $XFS_IO_PROG -c "add_enckey -k $keyid $add_enckey_args" $SCRATCH_MNT
98 echo -e "\n# Reading encrypted file"
101 echo -e "\n# Cleaning up"
103 _scratch_cycle_mount # Clear all keys
106 # Test with both v1 and v2 encryption policies.
107 test_with_policy_version 1
108 test_with_policy_version 2
110 # Now test that invalid fscrypt-provisioning keys can't be created, that
111 # fscrypt-provisioning keys can't be read back by userspace, and that the
112 # filesystem only accepts properly matching fscrypt-provisioning keys.
114 echo "# ================"
115 echo "# Validation tests"
116 echo "# ================"
118 echo -e "\n# Adding an invalid fscrypt-provisioning key fails"
119 echo "# ... bad type"
120 _add_fscrypt_provisioning_key desc 0 "$TEST_RAW_KEY"
121 echo "# ... bad type"
122 _add_fscrypt_provisioning_key desc 10000 "$TEST_RAW_KEY"
123 echo "# ... raw key too small"
124 _add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR ""
125 echo "# ... raw key too large"
126 _add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR \
127 "$TEST_RAW_KEY$TEST_RAW_KEY"
129 echo -e "\n# keyctl_read() doesn't work on fscrypt-provisioning keys"
130 keyid=$(_add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR \
132 $KEYCTL_PROG read $keyid
133 $KEYCTL_PROG unlink $keyid @s
135 echo -e "\n# Only keys with the correct fscrypt_provisioning_key_payload::type field can be added"
136 echo "# ... keyring key is v1, filesystem wants v2 key"
137 keyid=$(_add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR \
139 $XFS_IO_PROG -c "add_enckey -k $keyid" $SCRATCH_MNT
140 $KEYCTL_PROG unlink $keyid @s
142 echo "# ... keyring key is v2, filesystem wants v1 key"
143 keyid=$(_add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER \
145 $XFS_IO_PROG -c "add_enckey -k $keyid -d $TEST_KEY_DESCRIPTOR" $SCRATCH_MNT
146 $KEYCTL_PROG unlink $keyid @s
148 echo -e "\n# Only keys of type fscrypt-provisioning can be added"
149 keyid=$(head -c 64 /dev/urandom | $KEYCTL_PROG padd logon foo:desc @s)
150 $XFS_IO_PROG -c "add_enckey -k $keyid" $SCRATCH_MNT
151 $KEYCTL_PROG unlink $keyid @s