]> Git Repo - linux.git/blob - tools/testing/selftests/bpf/verify_sig_setup.sh
scsi: zfcp: Trace when request remove fails after qdio send fails
[linux.git] / tools / testing / selftests / bpf / verify_sig_setup.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 set -e
5 set -u
6 set -o pipefail
7
8 VERBOSE="${SELFTESTS_VERBOSE:=0}"
9 LOG_FILE="$(mktemp /tmp/verify_sig_setup.log.XXXXXX)"
10
11 x509_genkey_content="\
12 [ req ]
13 default_bits = 2048
14 distinguished_name = req_distinguished_name
15 prompt = no
16 string_mask = utf8only
17 x509_extensions = myexts
18
19 [ req_distinguished_name ]
20 CN = eBPF Signature Verification Testing Key
21
22 [ myexts ]
23 basicConstraints=critical,CA:FALSE
24 keyUsage=digitalSignature
25 subjectKeyIdentifier=hash
26 authorityKeyIdentifier=keyid
27 "
28
29 usage()
30 {
31         echo "Usage: $0 <setup|cleanup <existing_tmp_dir>"
32         exit 1
33 }
34
35 setup()
36 {
37         local tmp_dir="$1"
38
39         echo "${x509_genkey_content}" > ${tmp_dir}/x509.genkey
40
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
45
46         openssl x509 -in ${tmp_dir}/signing_key.pem -out \
47                 ${tmp_dir}/signing_key.der -outform der
48
49         key_id=$(cat ${tmp_dir}/signing_key.der | keyctl padd asymmetric ebpf_testing_key @s)
50
51         keyring_id=$(keyctl newring ebpf_testing_keyring @s)
52         keyctl link $key_id $keyring_id
53 }
54
55 cleanup() {
56         local tmp_dir="$1"
57
58         keyctl unlink $(keyctl search @s asymmetric ebpf_testing_key) @s
59         keyctl unlink $(keyctl search @s keyring ebpf_testing_keyring) @s
60         rm -rf ${tmp_dir}
61 }
62
63 catch()
64 {
65         local exit_code="$1"
66         local log_file="$2"
67
68         if [[ "${exit_code}" -ne 0 ]]; then
69                 cat "${log_file}" >&3
70         fi
71
72         rm -f "${log_file}"
73         exit ${exit_code}
74 }
75
76 main()
77 {
78         [[ $# -ne 2 ]] && usage
79
80         local action="$1"
81         local tmp_dir="$2"
82
83         [[ ! -d "${tmp_dir}" ]] && echo "Directory ${tmp_dir} doesn't exist" && exit 1
84
85         if [[ "${action}" == "setup" ]]; then
86                 setup "${tmp_dir}"
87         elif [[ "${action}" == "cleanup" ]]; then
88                 cleanup "${tmp_dir}"
89         else
90                 echo "Unknown action: ${action}"
91                 exit 1
92         fi
93 }
94
95 trap 'catch "$?" "${LOG_FILE}"' EXIT
96
97 if [[ "${VERBOSE}" -eq 0 ]]; then
98         # Save the stderr to 3 so that we can output back to
99         # it incase of an error.
100         exec 3>&2 1>"${LOG_FILE}" 2>&1
101 fi
102
103 main "$@"
104 rm -f "${LOG_FILE}"
This page took 0.041423 seconds and 4 git commands to generate.