-
Notifications
You must be signed in to change notification settings - Fork 3
/
verify_attestation.sh
executable file
·121 lines (108 loc) · 3.88 KB
/
verify_attestation.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
set -e
# Verify attestation and create secret to be injected
function usage() {
cat <<EOF
Usage:
verify-attestation [-p secret] [-m launch_measurement] -t tiktek_file -o ovmf_file -s sevtool -a api_minor -b api_major -c build_id -d policy
Verify attestation and create secret to be injected
-p secret file: secret to be injected (minimum of 8 bytes)
-m launch_measurement: launch measurement from QMP (result of query-sev-launch-measure)
-t tiktek_file: file where sev-tool stored the TIK/TEK combination, defaults to tmp_tk.bin
-o ovmf_file: location of OVMF file to calculate hash from, default is OVMF_CODE.fd
-s sevtool: location of sevtool, default is current directory
-a api_minor: api-minor SEV info from QMP (result of query-sev), default 22
-b api_major: api-major SEV info from QMP (result of query-sev), default 0
-c build_id: build-id from SEV info from QMP (result of query-sev), default 13
-d policy: policy from SEV info (result of query-sev), default 1
EOF
}
# default values
OVMF=OVMF.fd # Be sure to use the patched build of OVMF
TIKTEK="certs/tmp_tk.bin"
SEVTOOL=sevtool
API_MINOR=22
API_MAJOR=0
BUILD_ID=13
POLICY=1
while getopts "p:m:i:t:o:s:a:b:c:d:" OPT; do
case "${OPT}" in
p)
SECRET="${OPTARG}"
;;
m)
LAUNCH_MEASUREMENT="${OPTARG}"
;;
t)
TIKTEK="${OPTARG}"
;;
o)
OVMF="${OPTARG}"
;;
s)
SEVTOOL="${OPTARG}"
;;
a)
API_MINOR="${OPTARG}"
;;
b)
API_MAJOR="${OPTARG}"
;;
c)
BUILD_ID="${OPTARG}"
;;
d)
POLICY="${OPTARG}"
;;
*)
usage
exit 1
;;
esac
done
if [[ "${SECRET}" == "" || "${LAUNCH_MEASUREMENT}" == "" ]]; then
usage
exit 1
fi
# Calculate digest of OVMF.fd
ovmf_hash=$(sha256sum "${OVMF}" | awk '{print $1}')
echo "hash of $OVMF: $ovmf_hash"
# Dervie TIK from tiktek file
TIK=$(xxd -p "${TIKTEK}" | tr -d '\n' | tail -c 32)
echo "TIK: $TIK"
# Convert sev-info to hex
API_MINOR=$(printf "%x" ${API_MINOR})
API_MAJOR=$(printf "%x" ${API_MAJOR})
BUILD_ID=$(printf "%x" ${BUILD_ID})
POLICY=$(printf "%x" ${POLICY})
echo "SEV info: api_minor: ${API_MINOR}, api_major: ${API_MAJOR}, build_id: ${BUILD_ID}, policy: ${POLICY}"
# Derive mnonce and expected measurement from the launch_measurement
echo "${LAUNCH_MEASUREMENT}" | base64 -d | split -b 32
launch_measurement=$(xxd -p xaa | tr -d '\n')
mnonce=$(xxd -p xab | tr -d '\n')
# Run calc_measurement from sevtool
echo "Calculate expected measurement via sevtool"
echo "sudo ./sevtool --ofolder ./certs --calc_measurement 04 ${API_MAJOR} ${API_MINOR} ${BUILD_ID} ${POLICY} ${ovmf_hash} ${mnonce} ${TIK}"
sudo ./sevtool --ofolder ./certs --calc_measurement 04 $API_MAJOR $API_MINOR $BUILD_ID $POLICY $ovmf_hash $mnonce $TIK
if [ ! -f ./certs/calc_measurement_out.txt ]; then
echo "Measurement could not be calculated"
exit 1
fi
expected_measurement=$(< ./certs/calc_measurement_out.txt)
echo "Expected measurement: $expected_measurement"
echo "Launch measurement: $launch_measurement"
if [[ $expected_measurement != $launch_measurement ]]; then
echo "Measurement does not match. Attestation failed!"
exit 1
fi
# Create packaged secret and its header
cp "${SECRET}" ./certs/secret.txt
sudo ./sevtool --ofolder ./certs --package_secret
if [ ! -f ./certs/packaged_secret.bin ]; then
echo "Secret could not be created"
exit 1
fi
secret_base64=$(base64 -w 0 ./certs/packaged_secret.bin)
secret_header_base64=$(base64 -w 0 ./certs/packaged_secret_header.bin)
echo "Secret packaged successfully. Send the following message to the guest VM via qmp"
echo "{ \"execute\": \"sev-inject-launch-secret\", \"arguments\": { \"packet-header\": \"${secret_header_base64}\", \"secret\": \"${secret_base64}\"}}"