2 # SPDX-License-Identifier: GPL-2.0
8 VERBOSE="${SELFTESTS_VERBOSE:=0}"
9 LOG_FILE="$(mktemp /tmp/verify_sig_setup.log.XXXXXX)"
11 x509_genkey_content="\
14 distinguished_name = req_distinguished_name
16 string_mask = utf8only
17 x509_extensions = myexts
19 [ req_distinguished_name ]
20 CN = eBPF Signature Verification Testing Key
23 basicConstraints=critical,CA:FALSE
24 keyUsage=digitalSignature
25 subjectKeyIdentifier=hash
26 authorityKeyIdentifier=keyid
31 echo "Usage: $0 <setup|cleanup <existing_tmp_dir>"
39 echo "${x509_genkey_content}" > ${tmp_dir}/x509.genkey
41 openssl req -new -nodes -utf8 -sha256 -days 36500 \
42 -batch -x509 -config ${tmp_dir}/x509.genkey \
43 -outform PEM -out ${tmp_dir}/signing_key.pem \
44 -keyout ${tmp_dir}/signing_key.pem 2>&1
46 openssl x509 -in ${tmp_dir}/signing_key.pem -out \
47 ${tmp_dir}/signing_key.der -outform der
49 key_id=$(cat ${tmp_dir}/signing_key.der | keyctl padd asymmetric ebpf_testing_key @s)
51 keyring_id=$(keyctl newring ebpf_testing_keyring @s)
52 keyctl link $key_id $keyring_id
58 keyctl unlink $(keyctl search @s asymmetric ebpf_testing_key) @s
59 keyctl unlink $(keyctl search @s keyring ebpf_testing_keyring) @s
63 fsverity_create_sign_file() {
66 data_file=${tmp_dir}/data-file
67 sig_file=${tmp_dir}/sig-file
68 dd if=/dev/urandom of=$data_file bs=1 count=12345 2> /dev/null
69 fsverity sign --key ${tmp_dir}/signing_key.pem $data_file $sig_file
71 # We do not want to enable fsverity on $data_file yet. Try whether
72 # the file system support fsverity on a different file.
73 touch ${tmp_dir}/tmp-file
74 fsverity enable ${tmp_dir}/tmp-file
77 fsverity_enable_file() {
80 data_file=${tmp_dir}/data-file
81 fsverity enable $data_file
89 if [[ "${exit_code}" -ne 0 ]]; then
99 [[ $# -ne 2 ]] && usage
104 [[ ! -d "${tmp_dir}" ]] && echo "Directory ${tmp_dir} doesn't exist" && exit 1
106 if [[ "${action}" == "setup" ]]; then
108 elif [[ "${action}" == "cleanup" ]]; then
110 elif [[ "${action}" == "fsverity-create-sign" ]]; then
111 fsverity_create_sign_file "${tmp_dir}"
112 elif [[ "${action}" == "fsverity-enable" ]]; then
113 fsverity_enable_file "${tmp_dir}"
115 echo "Unknown action: ${action}"
120 trap 'catch "$?" "${LOG_FILE}"' EXIT
122 if [[ "${VERBOSE}" -eq 0 ]]; then
123 # Save the stderr to 3 so that we can output back to
124 # it incase of an error.
125 exec 3>&2 1>"${LOG_FILE}" 2>&1