Debian Codegraph @

src/pins/tpm2/tests/pin-tpm2-tests

raw
1 #!/bin/bash -x
2 # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
3 #
4 # Copyright (c) 2019 Red Hat, Inc.
5 # Author: Sergio Correia <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 TEST=$(basename "${0}")
22
23 validate_pcrs() {
24 local _pcr_bank="${1}"
25 local _pcrs="${2}"
26 [ -z "${_pcr_bank}" ] && return 1
27 [ -z "${_pcrs}" ] && return 0
28
29 local _fail=
30 local _pcrs_r=
31 case "${TPM2TOOLS_VERSION}" in
32 3) _pcrs_r="$(tpm2_pcrlist -L "${_pcr_bank}":"${_pcrs}" | grep -v "^${_pcr_bank}")" || _fail=$?;;
33 4|5) _pcrs_r=$(tpm2_pcrread "${_pcr_bank}":"${_pcrs}" | grep -v " ${_pcr_bank}") || _fail=$?;;
34 *) _fail=1
35 esac
36
37 if [ -n "${_fail}" ] || [ -z "${_pcrs_r}" ]; then
38 return 1
39 fi
40 return 0
41 }
42
43 decode_jwe() {
44 local jwe="${1}"
45
46 local coded
47 if ! coded=$(jose jwe fmt -i- <<< "${jwe}"); then
48 return 1
49 fi
50
51 coded=$(jose fmt -j- -g protected -u- <<< "${coded}" | tr -d '"')
52 jose b64 dec -i- <<< "${coded}"
53 }
54
55 test_pcr_ids() {
56 local orig="${1}"
57 local cfg="${2}"
58 local expected_pcr_ids="${3}"
59
60 local enc
61 if ! enc=$(echo -n "${orig}" | clevis encrypt tpm2 "${cfg}"); then
62 echo "${TEST}: encrypt failed for cfg: ${cfg}" >&2
63 return 1
64 fi
65
66 local pcr_ids
67 pcr_ids=$(decode_jwe "${enc}" \
68 | jose fmt -j- -Og clevis -Og tpm2 -Og pcr_ids -u- 2>/dev/null)
69
70 local dec
71 dec="$(echo "${enc}" | clevis decrypt)"
72
73 if [ "${orig}" != "${dec}" ]; then
74 echo "${TEST}: decoded text (${dec}) does not match original one (${orig})" >&2
75 return 1
76 fi
77
78 if [ "${pcr_ids}" != "${expected_pcr_ids}" ]; then
79 echo "${TEST}: pcr_ids (${pcr_ids}) do not match the expected (${expected_pcr_ids}) result." >&2
80 return 1
81 fi
82 }
83
84 test_enc_dec() {
85 local cfg="${1}"
86 output=$(echo Working | clevis encrypt tpm2 "${cfg}" | clevis decrypt)
87
88 if [ "$output" != "Working" ]; then
89 echo "Output after decrypting doesn't match: ${output} != 'Working'"
90 return 1
91 fi
92 }
93
94 test_enc_dec '{}' || exit 1
95 test_pcr_ids "Hi" '{}' "" || exit 1
96 test_pcr_ids "Hello" '{}' "" || exit 1
97 test_pcr_ids "Hi" '{ }' "" || exit 1
98 test_pcr_ids "Hi" '{"key": "ecc"}' "" || exit 1
99 test_pcr_ids "Hello" '{"key": "ecc"}' "" || exit 1
100
101 # Issue #103: now let's try a few different configs with both strings and
102 # arrays and check if we get the expected pcr_ids.
103
104 # Let's first make sure this would be a valid configuration.
105 _default_pcr_bank=$(tpm2_getcap pcrs |
106 awk '/^[[:space:]]*-[[:space:]]*([^:]+):[[:space:]]*\[[[:space:]]*[^][:space:]]/ \
107 {split($0, m, /[-:[:space:]]+/); print m[2]; exit}')
108 if [ -n "$_default_pcr_bank" ] && validate_pcrs "${_default_pcr_bank}" "4,16"; then
109 test_pcr_ids "Hi" '{"pcr_ids": "16"}' "16" || exit 1
110 test_pcr_ids "Hello" '{"pcr_ids": "16"}' "16" || exit 1
111 test_pcr_ids "Hi" '{"pcr_ids": ["16"]}' "16" || exit 1
112 test_pcr_ids "Hi" '{"pcr_ids": "4, 16"}' "4,16" || exit 1
113 test_pcr_ids "Hi" '{"pcr_ids": "4,16"}' "4,16" || exit 1
114 test_pcr_ids "Hello" '{"pcr_ids": "4,16"}' "4,16" || exit 1
115 test_pcr_ids "Hi" '{"pcr_ids": ["4,16"]}' "4,16" || exit 1
116 test_pcr_ids "Hi" '{"pcr_ids": [4,16]}' "4,16" || exit 1
117 test_pcr_ids "Hi" '{"pcr_ids": [4, 16]}' "4,16" || exit 1
118 test_pcr_ids "Hi" '{"pcr_ids": ["4","16"]}' "4,16" || exit 1
119 ! test_pcr_ids "Hi" '{"pcr_ids": ["4","16"]}' "foo bar" || exit 1
120 else
121 echo "Skipping tests related to issue#103 because the combination of pcr_bank and PCRs is invalid" >&2
122 fi
123
124 # Test with policies if we have the PIN rewrite available
125 if ! command -v clevis-pin-tpm2 >/dev/null 2>&1;
126 then
127 echo "No PIN rewrite available"
128 exit 0
129 fi
130 if ! command -v clevis-pin-tpm2-signtool >/dev/null 2>&1;
131 then
132 echo "No policy signtool available"
133 exit 0
134 fi
135
136 clevis-pin-tpm2-signtool >policy_working.json << EOP
137 ---
138 - policy_ref:
139 steps:
140 - PCRs:
141 hash_algorithm: sha256
142 selection:
143 - pcr_id: 21
144 value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
145 - pcr_id: 22
146 value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
147 EOP
148 clevis-pin-tpm2-signtool >policy_broken.json << EOP
149 ---
150 - policy_ref:
151 steps:
152 - PCRs:
153 hash_algorithm: sha256
154 selection:
155 - pcr_id: 21
156 value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
157 - pcr_id: 22
158 value: "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
159 EOP
160
161 test_enc_dec '{"policy_pubkey_path":"./publickey.json", "policy_ref": "", "policy_path": "./policy_working.json"}' || exit 1
162 ! test_enc_dec '{"policy_pubkey_path":"./publickey.json", "policy_ref": "", "policy_path": "./policy_broken.json"}' || exit 1