Debian Codegraph @

src/pins/pkcs11/clevis-pkcs11-common

raw symbols SCIP
1 #!/bin/bash
2 #
3 # Copyright (c) 2024 Red Hat, Inc.
4 # Author: Sergio Arroutbi <[email protected]>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 if [ "$1" = "--summary" ]; then
20 exit 1
21 fi
22
23 . clevis-luks-common-functions
24
25 serial_devices_array=""
26
27 URI_EXPECTED_FORMAT="pkcs11:"
28 DEFAULT_CRYPTTAB_FILE="/etc/crypttab"
29
30 clevis_detect_pkcs11_device() {
31 dracut_mode="${1:false}"
32 retry_mode="${2:false}"
33 if ! pkcs11_device=$(pkcs11-tool -L 2>/dev/null | grep "Slot" | head -1 | \
34 awk -F ":" '{print $2}' | sed -e 's@^ *@@g'); then
35 echo ""
36 return 1
37 fi
38
39 if ! pkcs11-tool -O 2>/dev/null 1>/dev/null; then
40 pkcs11_device=""
41 echo "No objects in PKCS11 device detected" >&2
42 fi
43
44 while [ -z "${pkcs11_device}" ]; do
45 if [ "${dracut_mode}" != true ]; then
46 module_paths=$(clevis_get_module_path_from_pkcs11_config "/etc/crypttab")
47 if [ -n "${module_paths}" ]; then
48 modules=$(echo ${module_paths} | tr ";" "\n")
49 for module in $modules; do
50 pkcs11_device=$(pkcs11-tool -L --module ${module} | grep "Slot" \
51 | head -1 | awk -F ":" '{print $2}' | sed -e 's@^ *@@g')
52 if [ -n "${pkcs11_device}" ]; then
53 break;
54 fi
55 done
56 fi
57 fi
58 if [ -z "${pkcs11_device}" ]; then
59 if [ "${retry_mode}" == true ]; then
60 option=$(systemd-ask-password --echo "Detected no PKCS#11 device, retry PKCS#11 detection? [yY/nN]")
61 if [ "${option}" == "N" ] || [ "${option}" == "n" ] ; then
62 echo ""
63 # Straight Forward Mode
64 return 0
65 fi
66 pkcs11_device=$(pkcs11-tool -L | grep "Slot" \
67 | head -1 | awk -F ":" '{print $2}' | sed -e 's@^ *@@g')
68 if ! pkcs11-tool -O 2>/dev/null 1>/dev/null; then
69 pkcs11_device=""
70 echo "No objects in PKCS11 device detected" >&2
71 fi
72 else
73 echo "${pkcs11_device}"
74 return 0
75 fi
76 fi
77 done
78 }
79
80 clevis_start_pcscd_server() {
81 if command -v pcscd; then
82 echo "clevis-pkcs11: starting pcscd if not available ..."
83 PCSCD_PID=$(ps auxf | grep "[p]cscd")
84 echo -e "clevis-pkcs11: pcscd running?:[${PCSCD_PID}]\n"
85 if ! ps auxf | grep "[p]cscd";
86 then
87 if pcscd --help | grep disable-polkit 1>/dev/null 2>/dev/null; then
88 echo "clevis-pkcs11: starting pcscd with --disable-polkit option ..."
89 pcscd --disable-polkit
90 else
91 echo "clevis-pkcs11: starting pcscd ..."
92 pcscd
93 fi
94 fi
95 fi
96 }
97
98 clevis_parse_devices_array() {
99 INPUT_ARRAY=$(pkcs11-tool -L | grep Slot)
100 counter=0
101 while read -r; do
102 serial=$(pkcs11-tool -L | sed -n "/Slot ${counter}/,/Slot $((counter+1))/p" | grep -i "serial num"| awk -F ":" '{print $2}' | tr -d ' ')
103 serial_devices_array[$counter]="${serial}"
104 ((counter++))
105 done <<< "${INPUT_ARRAY}"
106 }
107
108 clevis_get_serial_by_slot() {
109 clevis_parse_devices_array
110 serial=${serial_devices_array[$1]}
111 if [ -z "${serial}" ]; then
112 return 1
113 fi
114 echo "${serial}"
115 return 0
116 }
117
118 clevis_valid_pkcs11_uri() {
119 echo "$1" | grep -E "^${URI_EXPECTED_FORMAT}" >/dev/null 2>&1 || return 1
120 }
121
122 clevis_get_module_path_from_uri() {
123 echo "$1" | grep -E "module-path=" >/dev/null 2>&1 || return 1
124 echo "$1" | awk -F 'module-path=' '{print $2}' | awk -F ";" '{print $1}' \
125 | awk -F "?" '{print $1}'
126 }
127
128 clevis_get_module_path_from_pkcs11_config() {
129 CRYPTTABFILE="$1"
130 module_list=""
131 [ -z "${CRYPTTABFILE}" ] && CRYPTTABFILE="${DEFAULT_CRYPTTAB_FILE}"
132 while read -r line; do
133 uuid=$(echo "${line}" | awk '{print $2}')
134 if ! mapped_device=$(clevis_map_device "${uuid}"); then
135 continue
136 fi
137 # If no PKCS#11 configuration, advance to next device
138 if ! clevis luks list -d "${mapped_device}" | grep pkcs11 >/dev/null 2>&1; then
139 continue
140 fi
141 # Get configuration PKCS#11 URI
142 uri=$(clevis luks list -d "${mapped_device}" | awk -F '"uri":' '{print $2}' \
143 | awk -F '"' '{print $2}' | awk -F '"' '{print $1}')
144 if module_path=$(clevis_get_module_path_from_uri "${uri}"); then
145 if [ -z "${module_list}" ]; then
146 module_list="${module_path}"
147 else
148 module_list="${module_list};${module_path}"
149 fi
150 fi
151 done < <(grep -v "^#" "${CRYPTTABFILE}")
152 echo "${module_list}"
153 }
154
155 clevis_get_pin_value_from_uri() {
156 echo "$1" | grep -E "pin-value=" >/dev/null 2>&1 || return 1
157 echo "$1" | awk -F 'pin-value=' '{print $2}' | awk -F ";" '{print $1}'
158 }
159
160 clevis_get_slot_from_uri() {
161 echo "$1" | grep -E "slot-id=" >/dev/null 2>&1 || return 1
162 echo "$1" | awk -F 'slot-id=' '{print $2}' | awk -F ";" '{print $1}' \
163 | awk -F "?" '{print $1}'
164 }
165
166 clevis_percent_unencoding() {
167 echo "$1" | sed -E 's@%20@ @g' | sed -E 's@%21@!@g' | sed -E 's@%22@"@g' \
168 | sed -E 's@%23@#@g' | sed -E 's@%24@$@g' | sed -E 's@%25@%@g' \
169 | sed -E 's@%26@&@g' | sed -E "s@%27@'@g" | sed -E 's@%28@(@g' \
170 | sed -E 's@%29@)@g' | sed -E "s@%2A@*@g" | sed -E 's@%2B@+@g' \
171 | sed -E 's@%2C@,@g' | sed -E "s@%2F@/@g" | sed -E 's@%3A@:@g' \
172 | sed -E 's@%3B@;@g' | sed -E "s@%3D@=@g" | sed -E 's@%3F@?@g' \
173 | sed -E 's!%40!@!g' | sed -E "s@%5B@[@g" | sed -E 's@%5D@]@g'
174 }
175
176 clevis_get_model_from_uri() {
177 if ! echo "$1" | grep -E "model=" >/dev/null 2>&1; then
178 return 1
179 fi
180 model=$(echo "$1" | awk -F 'model=' '{print $2}' | awk -F ";" '{print $1}' \
181 | awk -F "?" '{print $1}')
182 clevis_percent_unencoding "${model}"
183 }
184
185 clevis_get_token_from_uri() {
186 if ! echo "$1" | grep -E "token=" >/dev/null 2>&1; then
187 return 1
188 fi
189 token=$(echo "$1" | awk -F 'token=' '{print $2}' | awk -F ";" '{print $1}' \
190 | awk -F "?" '{print $1}')
191 clevis_percent_unencoding "${token}"
192 }
193
194 clevis_get_serial_from_uri() {
195 if ! echo "$1" | grep -E "serial=" >/dev/null 2>&1; then
196 return 1
197 fi
198 serial=$(echo "$1" | awk -F 'serial=' '{print $2}' | awk -F ";" '{print $1}' \
199 | awk -F "?" '{print $1}')
200 clevis_percent_unencoding "${serial}"
201 }
202
203 clevis_get_max_pkcs11_slot() {
204 if [ -z "${1}" ]; then
205 module_opt=""
206 else
207 module_opt="--module ${1}"
208 fi
209 if ! pkcs11-tool -L ${module_opt} | grep -i "^slot" | awk '{print $2}' | tail -1; then
210 echo "-1"
211 fi
212 }
213
214 clevis_pkcs11_filter_uri() {
215 echo "$1" | sed -E 's@module-path=[a-z,A-Z,0-9,/,.]{1,};{0,1}@@' | sed -E 's@;$@@g'
216 }
217
218 clevis_get_slot_by_serial_from_uri() {
219 module_path=""
220 if ! module_path=$(clevis_get_module_path_from_uri "${1}"); then
221 module_opt=""
222 else
223 module_opt=" --module ${module_path}"
224 fi
225 serialuri=$(clevis_get_serial_from_uri "${1}")
226 for ((s=0; s<=$(clevis_get_max_pkcs11_slot "${module_path}"); s++))
227 do
228 if ! pkcs11-tool -O --slot-index=${s} 1>/dev/null 2>/dev/null; then
229 continue
230 fi
231 serial=$(clevis_get_serial_by_slot "${s}")
232 if [ "${serial}" = "${serialuri}" ]; then
233 echo "${s}"
234 return 0
235 fi
236 done
237 return 1
238 }
239
240 clevis_get_slot_by_serial_and_token_from_uri() {
241 module_path=""
242 if ! module_path=$(clevis_get_module_path_from_uri "${1}"); then
243 module_opt=""
244 else
245 module_opt=" --module ${module_path}"
246 fi
247 serialuri=$(clevis_get_serial_from_uri "${1}")
248 tokenuri="$(clevis_get_token_from_uri "${1}")"
249 for ((s=0; s<=$(clevis_get_max_pkcs11_slot "${module_path}"); s++))
250 do
251 if pkcs11-tool -L ${module_opt} 2>/dev/null | \
252 grep -i 'serial num' | head -$((s+1)) | tail -1 | \
253 awk -F ':' '{print $2}' | tr -d ' ' | \
254 grep "${serialuri}" 2>/dev/null 1>/dev/null; then
255 pkcs11tokenuri="$(pkcs11-tool -L ${module_opt} 2>/dev/null \
256 | grep -i 'token label' | head -$((s+1)) | tail -1 \
257 | awk -F ':' '{print $2}' | sed -E 's@^ {0,}@@g')"
258 if [ "${tokenuri}" = "${pkcs11tokenuri}" ]; then
259 echo "${s}"
260 return 0
261 fi
262 fi
263 done
264 return 1
265 }
266
267 clevis_get_pkcs11_pubkey_slot_from_uri() {
268 module_path=""
269 if ! module_path=$(clevis_get_module_path_from_uri "${1}"); then
270 module_opt=""
271 else
272 module_opt=" --module ${module_path}"
273 fi
274 uri=$(clevis_pkcs11_filter_uri "${1}")
275 for ((s=0; s<=$(clevis_get_max_pkcs11_slot "${module_path}"); s++))
276 do
277 if pkcs11-tool -O ${module_opt} --slot-index ${s} --type pubkey 2>/dev/null \
278 | grep -i 'uri:' | awk -F 'uri:' '{print $2}' | tr -d ' ' \
279 | grep "${uri}" 2>/dev/null 1>/dev/null; then
280 echo "${s}"
281 return 0
282 fi
283 done
284 return 1
285 }
286
287 clevis_get_pkcs11_final_slot_from_uri() {
288 if slot=$(clevis_get_slot_from_uri "${1}"); then
289 echo "${slot}"
290 return 0
291 fi
292 if [ -z "${slot}" ]; then
293 if slot=$(clevis_get_pkcs11_pubkey_slot_from_uri "${1}"); then
294 echo "${slot}"
295 return 0
296 fi
297 fi
298 if [ -z "${slot}" ]; then
299 if slot=$(clevis_get_slot_by_serial_and_token_from_uri "${1}"); then
300 echo "${slot}"
301 return 0
302 fi
303 fi
304 if [ -z "${slot}" ]; then
305 if slot=$(clevis_get_slot_by_serial_from_uri "${1}"); then
306 echo "${slot}"
307 return 0
308 fi
309 fi
310 return 1
311 }