| 1 | #!/bin/bashscip-shell filesystem . . `/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 [scip-shell system . . `[`. "$1" = "--summary" ]; then |
| 20 | exit 1 |
| 21 | fi |
| 22 | |
| 23 | . clevis-luks-common-functionsscip-shell source . . clevis-luks-common-functions. |
| 24 | |
| 25 | serial_devices_array="" |
| 26 | |
| 27 | URI_EXPECTED_FORMAT="pkcs11:" |
| 28 | DEFAULT_CRYPTTAB_FILE="/etc/crypttabscip-shell filesystem . . `/etc/crypttab`." |
| 29 | |
| 30 | clevis_detect_pkcs11_devicescip-shell shell clevis 23-1 clevis_detect_pkcs11_device().() { |
| 31 | dracut_mode="${1:false}" |
| 32 | retry_mode="${2:false}" |
| 33 | if ! pkcs11_device=$(pkcs11-tool -L 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. "Slot" | headscip-shell system . . head. -1 | \ |
| 34 | awk -F ":" '{print $2}' | sed -e 's@^ *@@g'); then |
| 35 | echoscip-shell system . . echo. "" |
| 36 | return 1 |
| 37 | fi |
| 38 | |
| 39 | if ! pkcs11-tool -O 2>/dev/nullscip-shell filesystem . . `/dev/null`. 1>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 40 | pkcs11_device="" |
| 41 | echoscip-shell system . . echo. "No objects in PKCS11 device detected" >&2 |
| 42 | fi |
| 43 | |
| 44 | while [scip-shell system . . `[`. -z "${pkcs11_device}" ]; do |
| 45 | if [scip-shell system . . `[`. "${dracut_mode}" != true ]; then |
| 46 | module_paths=$(clevis_get_module_path_from_pkcs11_config "/etc/crypttabscip-shell filesystem . . `/etc/crypttab`.") |
| 47 | if [scip-shell system . . `[`. -n "${module_paths}" ]; then |
| 48 | modules=$(echoscip-shell system . . echo. ${module_paths} | trscip-shell system . . tr. ";" "\n") |
| 49 | for module in $modules; do |
| 50 | pkcs11_device=$(pkcs11-tool -L --module ${module} | grepscip-shell system . . grep. "Slot" \ |
| 51 | | head -1 | awk -F ":" '{print $2}' | sed -e 's@^ *@@g') |
| 52 | if [scip-shell system . . `[`. -n "${pkcs11_device}" ]; then |
| 53 | break; |
| 54 | fi |
| 55 | done |
| 56 | fi |
| 57 | fi |
| 58 | if [scip-shell system . . `[`. -z "${pkcs11_device}" ]; then |
| 59 | if [scip-shell system . . `[`. "${retry_mode}" == true ]; then |
| 60 | option=$(systemd-ask-password --echo "Detected no PKCS#11 device, retry PKCS#11 detection? [yY/nN]") |
| 61 | if [scip-shell system . . `[`. "${option}" == "N" ] || [scip-shell system . . `[`. "${option}" == "n" ] ; then |
| 62 | echoscip-shell system . . echo. "" |
| 63 | # Straight Forward Mode |
| 64 | return 0 |
| 65 | fi |
| 66 | pkcs11_device=$(pkcs11-tool -L | grepscip-shell system . . grep. "Slot" \ |
| 67 | | head -1 | awk -F ":" '{print $2}' | sed -e 's@^ *@@g') |
| 68 | if ! pkcs11-tool -O 2>/dev/nullscip-shell filesystem . . `/dev/null`. 1>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 69 | pkcs11_device="" |
| 70 | echoscip-shell system . . echo. "No objects in PKCS11 device detected" >&2 |
| 71 | fi |
| 72 | else |
| 73 | echoscip-shell system . . echo. "${pkcs11_device}" |
| 74 | return 0 |
| 75 | fi |
| 76 | fi |
| 77 | done |
| 78 | } |
| 79 | |
| 80 | clevis_start_pcscd_serverscip-shell shell clevis 23-1 clevis_start_pcscd_server().() { |
| 81 | if command -v pcscd; then |
| 82 | echoscip-shell system . . echo. "clevis-pkcs11: starting pcscd if not available ..." |
| 83 | PCSCD_PID=$(psscip-shell system . . ps. auxf | grepscip-shell system . . grep. "[p]cscd") |
| 84 | echoscip-shell system . . echo. -e "clevis-pkcs11: pcscd running?:[${PCSCD_PID}]\n" |
| 85 | if ! psscip-shell system . . ps. auxf | grepscip-shell system . . grep. "[p]cscd"; |
| 86 | then |
| 87 | if pcscd --help | grepscip-shell system . . grep. disable-polkit 1>/dev/nullscip-shell filesystem . . `/dev/null`. 2>/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 88 | echoscip-shell system . . echo. "clevis-pkcs11: starting pcscd with --disable-polkit option ..." |
| 89 | pcscd --disable-polkit |
| 90 | else |
| 91 | echoscip-shell system . . echo. "clevis-pkcs11: starting pcscd ..." |
| 92 | pcscd |
| 93 | fi |
| 94 | fi |
| 95 | fi |
| 96 | } |
| 97 | |
| 98 | clevis_parse_devices_arrayscip-shell shell clevis 23-1 clevis_parse_devices_array().() { |
| 99 | INPUT_ARRAY=$(pkcs11-tool -L | grepscip-shell system . . grep. Slot) |
| 100 | counter=0 |
| 101 | while read -r; do |
| 102 | serial=$(pkcs11-tool -L | sedscip-shell system . . sed. -n "/Slot ${counter}/,/Slot $((counter+1))/p" | grepscip-shell system . . grep. -i "serial num"| awkscip-shell system . . awk. -F ":" '{print $2}' | trscip-shell system . . tr. -d ' ') |
| 103 | serial_devices_array[$counter]="${serial}" |
| 104 | ((counter++)) |
| 105 | done <<< "${INPUT_ARRAY}" |
| 106 | } |
| 107 | |
| 108 | clevis_get_serial_by_slotscip-shell shell clevis 23-1 clevis_get_serial_by_slot().() { |
| 109 | clevis_parse_devices_arrayscip-shell shell clevis 23-1 clevis_parse_devices_array(). |
| 110 | serial=${serial_devices_array[$1]} |
| 111 | if [scip-shell system . . `[`. -z "${serial}" ]; then |
| 112 | return 1 |
| 113 | fi |
| 114 | echoscip-shell system . . echo. "${serial}" |
| 115 | return 0 |
| 116 | } |
| 117 | |
| 118 | clevis_valid_pkcs11_uriscip-shell shell clevis 23-1 clevis_valid_pkcs11_uri().() { |
| 119 | echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -E "^${URI_EXPECTED_FORMAT}" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || return 1 |
| 120 | } |
| 121 | |
| 122 | clevis_get_module_path_from_uriscip-shell shell clevis 23-1 clevis_get_module_path_from_uri().() { |
| 123 | echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -E "module-path=" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || return 1 |
| 124 | echoscip-shell system . . echo. "$1" | awkscip-shell system . . awk. -F 'module-path=' '{print $2}' | awkscip-shell system . . awk. -F ";" '{print $1}' \ |
| 125 | | awkscip-shell system . . awk. -F "?" '{print $1}' |
| 126 | } |
| 127 | |
| 128 | clevis_get_module_path_from_pkcs11_configscip-shell shell clevis 23-1 clevis_get_module_path_from_pkcs11_config().() { |
| 129 | CRYPTTABFILE="$1" |
| 130 | module_list="" |
| 131 | [scip-shell system . . `[`. -z "${CRYPTTABFILE}" ] && CRYPTTABFILE="${DEFAULT_CRYPTTAB_FILE}" |
| 132 | while read -r line; do |
| 133 | uuid=$(echoscip-shell system . . echo. "${line}" | awkscip-shell system . . 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}" | grepscip-shell system . . grep. pkcs11 >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 139 | continue |
| 140 | fi |
| 141 | # Get configuration PKCS#11 URI |
| 142 | uri=$(clevis luks list -d "${mapped_device}" | awkscip-shell system . . awk. -F '"uri":' '{print $2}' \ |
| 143 | | awk -F '"' '{print $2}' | awk -F '"' '{print $1}') |
| 144 | if module_path=$(clevis_get_module_path_from_uriscip-shell shell clevis 23-1 clevis_get_module_path_from_uri(). "${uri}"); then |
| 145 | if [scip-shell system . . `[`. -z "${module_list}" ]; then |
| 146 | module_list="${module_path}" |
| 147 | else |
| 148 | module_list="${module_list};${module_path}" |
| 149 | fi |
| 150 | fi |
| 151 | done < <(grepscip-shell system . . grep. -v "^#" "${CRYPTTABFILE}") |
| 152 | echoscip-shell system . . echo. "${module_list}" |
| 153 | } |
| 154 | |
| 155 | clevis_get_pin_value_from_uriscip-shell shell clevis 23-1 clevis_get_pin_value_from_uri().() { |
| 156 | echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -E "pin-value=" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || return 1 |
| 157 | echoscip-shell system . . echo. "$1" | awkscip-shell system . . awk. -F 'pin-value=' '{print $2}' | awkscip-shell system . . awk. -F ";" '{print $1}' |
| 158 | } |
| 159 | |
| 160 | clevis_get_slot_from_uriscip-shell shell clevis 23-1 clevis_get_slot_from_uri().() { |
| 161 | echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -E "slot-id=" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || return 1 |
| 162 | echoscip-shell system . . echo. "$1" | awkscip-shell system . . awk. -F 'slot-id=' '{print $2}' | awkscip-shell system . . awk. -F ";" '{print $1}' \ |
| 163 | | awkscip-shell system . . awk. -F "?" '{print $1}' |
| 164 | } |
| 165 | |
| 166 | clevis_percent_unencodingscip-shell shell clevis 23-1 clevis_percent_unencoding().() { |
| 167 | echoscip-shell system . . echo. "$1" | sedscip-shell system . . sed. -E 's@%20@ @g' | sedscip-shell system . . sed. -E 's@%21@!@g' | sedscip-shell system . . sed. -E 's@%22@"@g' \ |
| 168 | | sedscip-shell system . . sed. -E 's@%23@#@g' | sedscip-shell system . . sed. -E 's@%24@$@g' | sedscip-shell system . . sed. -E 's@%25@%@g' \ |
| 169 | | sedscip-shell system . . sed. -E 's@%26@&@g' | sedscip-shell system . . sed. -E "s@%27@'@g" | sedscip-shell system . . sed. -E 's@%28@(@g' \ |
| 170 | | sedscip-shell system . . sed. -E 's@%29@)@g' | sedscip-shell system . . sed. -E "s@%2A@*@g" | sedscip-shell system . . sed. -E 's@%2B@+@g' \ |
| 171 | | sedscip-shell system . . sed. -E 's@%2C@,@g' | sedscip-shell system . . sed. -E "s@%2F@/@g" | sedscip-shell system . . sed. -E 's@%3A@:@g' \ |
| 172 | | sedscip-shell system . . sed. -E 's@%3B@;@g' | sedscip-shell system . . sed. -E "s@%3D@=@g" | sedscip-shell system . . sed. -E 's@%3F@?@g' \ |
| 173 | | sedscip-shell system . . sed. -E 's!%40!@!g' | sedscip-shell system . . sed. -E "s@%5B@[@g" | sedscip-shell system . . sed. -E 's@%5D@]@g' |
| 174 | } |
| 175 | |
| 176 | clevis_get_model_from_uriscip-shell shell clevis 23-1 clevis_get_model_from_uri().() { |
| 177 | if ! echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -E "model=" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 178 | return 1 |
| 179 | fi |
| 180 | model=$(echoscip-shell system . . echo. "$1" | awkscip-shell system . . awk. -F 'model=' '{print $2}' | awkscip-shell system . . awk. -F ";" '{print $1}' \ |
| 181 | | awk -F "?" '{print $1}') |
| 182 | clevis_percent_unencodingscip-shell shell clevis 23-1 clevis_percent_unencoding(). "${model}" |
| 183 | } |
| 184 | |
| 185 | clevis_get_token_from_uriscip-shell shell clevis 23-1 clevis_get_token_from_uri().() { |
| 186 | if ! echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -E "token=" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 187 | return 1 |
| 188 | fi |
| 189 | token=$(echoscip-shell system . . echo. "$1" | awkscip-shell system . . awk. -F 'token=' '{print $2}' | awkscip-shell system . . awk. -F ";" '{print $1}' \ |
| 190 | | awk -F "?" '{print $1}') |
| 191 | clevis_percent_unencodingscip-shell shell clevis 23-1 clevis_percent_unencoding(). "${token}" |
| 192 | } |
| 193 | |
| 194 | clevis_get_serial_from_uriscip-shell shell clevis 23-1 clevis_get_serial_from_uri().() { |
| 195 | if ! echoscip-shell system . . echo. "$1" | grepscip-shell system . . grep. -E "serial=" >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 196 | return 1 |
| 197 | fi |
| 198 | serial=$(echoscip-shell system . . echo. "$1" | awkscip-shell system . . awk. -F 'serial=' '{print $2}' | awkscip-shell system . . awk. -F ";" '{print $1}' \ |
| 199 | | awk -F "?" '{print $1}') |
| 200 | clevis_percent_unencodingscip-shell shell clevis 23-1 clevis_percent_unencoding(). "${serial}" |
| 201 | } |
| 202 | |
| 203 | clevis_get_max_pkcs11_slotscip-shell shell clevis 23-1 clevis_get_max_pkcs11_slot().() { |
| 204 | if [scip-shell system . . `[`. -z "${1}" ]; then |
| 205 | module_opt="" |
| 206 | else |
| 207 | module_opt="--module ${1}" |
| 208 | fi |
| 209 | if ! pkcs11-tool -L ${module_opt} | grepscip-shell system . . grep. -i "^slot" | awkscip-shell system . . awk. '{print $2}' | tailscip-shell system . . tail. -1; then |
| 210 | echoscip-shell system . . echo. "-1" |
| 211 | fi |
| 212 | } |
| 213 | |
| 214 | clevis_pkcs11_filter_uriscip-shell shell clevis 23-1 clevis_pkcs11_filter_uri().() { |
| 215 | echoscip-shell system . . echo. "$1" | sedscip-shell system . . sed. -E 's@module-path=[a-z,A-Z,0-9,/,.]{1,};{0,1}@@' | sedscip-shell system . . sed. -E 's@;$@@g' |
| 216 | } |
| 217 | |
| 218 | clevis_get_slot_by_serial_from_uriscip-shell shell clevis 23-1 clevis_get_slot_by_serial_from_uri().() { |
| 219 | module_path="" |
| 220 | if ! module_path=$(clevis_get_module_path_from_uriscip-shell shell clevis 23-1 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_uriscip-shell shell clevis 23-1 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_uriscip-shell shell clevis 23-1 clevis_get_slot_by_serial_and_token_from_uri().() { |
| 241 | module_path="" |
| 242 | if ! module_path=$(clevis_get_module_path_from_uriscip-shell shell clevis 23-1 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_uriscip-shell shell clevis 23-1 clevis_get_serial_from_uri(). "${1}") |
| 248 | tokenuri="$(clevis_get_token_from_uriscip-shell shell clevis 23-1 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_uriscip-shell shell clevis 23-1 clevis_get_pkcs11_pubkey_slot_from_uri().() { |
| 268 | module_path="" |
| 269 | if ! module_path=$(clevis_get_module_path_from_uriscip-shell shell clevis 23-1 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_uriscip-shell shell clevis 23-1 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_uriscip-shell shell clevis 23-1 clevis_get_pkcs11_final_slot_from_uri().() { |
| 288 | if slot=$(clevis_get_slot_from_uriscip-shell shell clevis 23-1 clevis_get_slot_from_uri(). "${1}"); then |
| 289 | echoscip-shell system . . echo. "${slot}" |
| 290 | return 0 |
| 291 | fi |
| 292 | if [scip-shell system . . `[`. -z "${slot}" ]; then |
| 293 | if slot=$(clevis_get_pkcs11_pubkey_slot_from_uriscip-shell shell clevis 23-1 clevis_get_pkcs11_pubkey_slot_from_uri(). "${1}"); then |
| 294 | echoscip-shell system . . echo. "${slot}" |
| 295 | return 0 |
| 296 | fi |
| 297 | fi |
| 298 | if [scip-shell system . . `[`. -z "${slot}" ]; then |
| 299 | if slot=$(clevis_get_slot_by_serial_and_token_from_uriscip-shell shell clevis 23-1 clevis_get_slot_by_serial_and_token_from_uri(). "${1}"); then |
| 300 | echoscip-shell system . . echo. "${slot}" |
| 301 | return 0 |
| 302 | fi |
| 303 | fi |
| 304 | if [scip-shell system . . `[`. -z "${slot}" ]; then |
| 305 | if slot=$(clevis_get_slot_by_serial_from_uriscip-shell shell clevis 23-1 clevis_get_slot_by_serial_from_uri(). "${1}"); then |
| 306 | echoscip-shell system . . echo. "${slot}" |
| 307 | return 0 |
| 308 | fi |
| 309 | fi |
| 310 | return 1 |
| 311 | } |