fstests: move test group info to test files
[xfstests-dev.git] / tests / generic / 593
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright 2019 Google LLC
4 #
5 # FS QA Test No. 593
6 #
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.
10 #
11 . ./common/preamble
12 _begin_fstest auto quick encrypt
13
14 # Import common functions.
15 . ./common/filter
16 . ./common/encrypt
17
18 # real QA test starts here
19 _supported_fs generic
20 _require_scratch_encryption -v 2
21 _require_command "$KEYCTL_PROG" keyctl
22
23 _new_session_keyring
24 _scratch_mkfs_encrypted &>> $seqres.full
25 _scratch_mount
26 _require_add_enckey_by_key_id $SCRATCH_MNT
27
28 test_with_policy_version()
29 {
30         local vers=$1
31         local dir=$SCRATCH_MNT/dir
32         local keyid
33
34         echo
35         echo "# =========================="
36         echo "# Test with policy version $vers"
37         echo "# =========================="
38
39         case $vers in
40         1)
41                 local keytype=$FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR
42                 local keyspec=$TEST_KEY_DESCRIPTOR
43                 local add_enckey_args="-d $TEST_KEY_DESCRIPTOR"
44                 ;;
45         2)
46                 local keytype=$FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER
47                 local keyspec=$TEST_KEY_IDENTIFIER
48                 local add_enckey_args=""
49                 ;;
50         *)
51                 _fail "Unknown policy version: $vers"
52                 ;;
53         esac
54
55         # First add the key in the regular way (raw key given directly), create
56         # an encrypted file with some contents, and remove the key.  After this,
57         # the encrypted file should no longer be readable.
58
59         echo -e "\n# Adding key to filesystem"
60         _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY" $add_enckey_args
61
62         echo -e "\n# Creating encrypted file"
63         mkdir $dir
64         _set_encpolicy $dir $keyspec
65         echo "contents" > $dir/file
66
67         echo -e "\n# Removing key from filesystem"
68         _rm_enckey $SCRATCH_MNT $keyspec
69         cat $dir/file |& _filter_scratch
70
71         # Now we should be able to add the key back via an fscrypt-provisioning
72         # key which contains the raw key, instead of providing the raw key
73         # directly.  After this, the encrypted file should be readable again.
74
75         echo -e "\n# Adding fscrypt-provisioning key"
76         keyid=$(_add_fscrypt_provisioning_key $keyspec $keytype "$TEST_RAW_KEY")
77
78         echo -e "\n# Adding key to filesystem via fscrypt-provisioning key"
79         $XFS_IO_PROG -c "add_enckey -k $keyid $add_enckey_args" $SCRATCH_MNT
80
81         echo -e "\n# Reading encrypted file"
82         cat $dir/file
83
84         echo -e "\n# Cleaning up"
85         rm -rf $dir
86         _scratch_cycle_mount    # Clear all keys
87 }
88
89 # Test with both v1 and v2 encryption policies.
90 test_with_policy_version 1
91 test_with_policy_version 2
92
93 # Now test that invalid fscrypt-provisioning keys can't be created, that
94 # fscrypt-provisioning keys can't be read back by userspace, and that the
95 # filesystem only accepts properly matching fscrypt-provisioning keys.
96 echo
97 echo "# ================"
98 echo "# Validation tests"
99 echo "# ================"
100
101 echo -e "\n# Adding an invalid fscrypt-provisioning key fails"
102 echo "# ... bad type"
103 _add_fscrypt_provisioning_key desc 0 "$TEST_RAW_KEY"
104 echo "# ... bad type"
105 _add_fscrypt_provisioning_key desc 10000 "$TEST_RAW_KEY"
106 echo "# ... raw key too small"
107 _add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR ""
108 echo "# ... raw key too large"
109 _add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR \
110         "$TEST_RAW_KEY$TEST_RAW_KEY"
111
112 echo -e "\n# keyctl_read() doesn't work on fscrypt-provisioning keys"
113 keyid=$(_add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR \
114         "$TEST_RAW_KEY")
115 $KEYCTL_PROG read $keyid
116 $KEYCTL_PROG unlink $keyid @s
117
118 echo -e "\n# Only keys with the correct fscrypt_provisioning_key_payload::type field can be added"
119 echo "# ... keyring key is v1, filesystem wants v2 key"
120 keyid=$(_add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR \
121         "$TEST_RAW_KEY")
122 $XFS_IO_PROG -c "add_enckey -k $keyid" $SCRATCH_MNT
123 $KEYCTL_PROG unlink $keyid @s
124
125 echo "# ... keyring key is v2, filesystem wants v1 key"
126 keyid=$(_add_fscrypt_provisioning_key desc $FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER \
127         "$TEST_RAW_KEY")
128 $XFS_IO_PROG -c "add_enckey -k $keyid -d $TEST_KEY_DESCRIPTOR" $SCRATCH_MNT
129 $KEYCTL_PROG unlink $keyid @s
130
131 echo -e "\n# Only keys of type fscrypt-provisioning can be added"
132 keyid=$(head -c 64 /dev/urandom | $KEYCTL_PROG padd logon foo:desc @s)
133 $XFS_IO_PROG -c "add_enckey -k $keyid" $SCRATCH_MNT
134 $KEYCTL_PROG unlink $keyid @s
135
136 # success, all done
137 status=0
138 exit