| 1 | #!/bin/bash -xe |
| 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 | # shellcheck disable=SC1091 |
| 20 | . pkcs11-common-tests |
| 21 | . luks-common-test-functions |
| 22 | . clevis-luks-common-functions |
| 23 | . clevis-pkcs11-common |
| 24 | |
| 25 | on_exit() { |
| 26 | exit_status=$? |
| 27 | [ -d "$TMP" ] && rm -rf "$TMP" |
| 28 | exit "${exit_status}" |
| 29 | } |
| 30 | |
| 31 | if [[ ! -f "${P11LIB}" ]]; then |
| 32 | skip_test "WARNING: The SoftHSM is not installed. Can not run this test" |
| 33 | fi |
| 34 | |
| 35 | trap 'on_exit' EXIT |
| 36 | |
| 37 | TMP="$(mktemp -d)" |
| 38 | |
| 39 | softhsm_lib_setup |
| 40 | test "$?" == 0 |
| 41 | |
| 42 | SECRET_WORD="secret" |
| 43 | SUPPORTED_MECHANISM="RSA-PKCS" |
| 44 | CLEVIS_PIN="pkcs11" |
| 45 | DEFAULT_SLOT="0" |
| 46 | |
| 47 | sword=$(echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 48 | "{\"uri\":\"pkcs11:module-path=${P11LIB}?pin-value=${PIN}\"\ |
| 49 | ,\"mechanism\":\"${SUPPORTED_MECHANISM}\"}" | clevis decrypt) |
| 50 | test "${sword}" == "${SECRET_WORD}" |
| 51 | |
| 52 | sword=$(echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 53 | "{\"uri\":\"pkcs11:module-path=${P11LIB};slot=${DEFAULT_SLOT}\ |
| 54 | ?pin-value=${PIN}\",\"mechanism\":\"${SUPPORTED_MECHANISM}\"}" \ |
| 55 | | clevis decrypt) |
| 56 | test "${sword}" == "${SECRET_WORD}" |
| 57 | |
| 58 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 59 | "{\"uri\":\"pkcs11:module-path=${P11LIB}?pin-value=${PIN}\" \ |
| 60 | ,\"mechanism\":\"INVALID_MECHANISM\"}" 2>/dev/null |
| 61 | |
| 62 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 63 | "{\"uri\":\"pkcs11:module-path=${P11LIB}?pin-value=${PIN}\"\ |
| 64 | ,\"mechanism\":\"INVALID_MECHANISM\"}" 2>/dev/null |
| 65 | |
| 66 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 67 | "{\"uri\":\"pkcs11:module-path=${P11LIB};slot=1?pin-value=${PIN}\" \ |
| 68 | ,\"mechanism\":\"${SUPPORTED_MECHANISM}\"}" 2>/dev/null |
| 69 | |
| 70 | sword=$(echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 71 | "{\"uri\":\"pkcs11:module-path=${P11LIB}?pin-value=INVALID_PIN\"\ |
| 72 | ,\"mechanism\":\"${SUPPORTED_MECHANISM}\"}" 2>/dev/null | clevis decrypt 2>/dev/null || :) |
| 73 | test "${sword}" != "${SECRET_WORD}" 2>/dev/null |
| 74 | |
| 75 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 76 | "{\"uri\":\"pkcs11:module-path=/usr/lib/wrong_modulepath.so?pin-value=${PIN}\"\ |
| 77 | ,\"mechanism\":\"${SUPPORTED_MECHANISM}\"}" 2>/dev/null |
| 78 | |
| 79 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 \ |
| 80 | "{\"uri\":\"pkcs11:?pin-value=${PIN}\"\ |
| 81 | ,\"mechanism\":\"${SUPPORTED_MECHANISM}\"}" 2>/dev/null |
| 82 | |
| 83 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 "{\"uri\":\"pkcs12:\"}" \ |
| 84 | 2>/dev/null |
| 85 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 "{\"uri\":\":\"}" 2>/dev/null |
| 86 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 "{\"uri\":\"\"}" 2>/dev/null |
| 87 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 "{\"uri\":}" 2>/dev/null |
| 88 | ! echo "${SECRET_WORD}" | clevis encrypt pkcs11 "{}" 2>/dev/null |
| 89 | |
| 90 | # Let's try some bindings |
| 91 | # LUKS2. |
| 92 | DEV="${TMP}/luks2-device" |
| 93 | new_device "luks2" "${DEV}" |
| 94 | |
| 95 | CFG=$(printf '{"uri": "pkcs11:module-path=%s?pin-value=%s", "mechanism":"%s"}' \ |
| 96 | "${P11LIB}" "${PIN}" "${SUPPORTED_MECHANISM}") |
| 97 | if ! clevis luks bind -f -d "${DEV}" "${CLEVIS_PIN}" "${CFG}" <<< \ |
| 98 | "${DEFAULT_PASS}"; then |
| 99 | error "${TEST}: Binding is expected to succeed when given a correct \ |
| 100 | (${DEFAULT_PASS}) password." |
| 101 | fi |
| 102 | |
| 103 | SLT=1 |
| 104 | if ! read -r slot pin cfg < <(clevis luks list -d "${DEV}" -s "${SLT}"); then |
| 105 | error "${TEST}: clevis luks list is expected to succeed for device(${DEV}) \ |
| 106 | and slot (${SLT})" |
| 107 | fi |
| 108 | |
| 109 | if [[ "${slot}" != "${SLT}:" ]]; then |
| 110 | error "${TEST}: slot (${slot}) is expected to be ${SLT}" |
| 111 | fi |
| 112 | |
| 113 | if [[ "${pin}" != "${CLEVIS_PIN}" ]]; then |
| 114 | error "${TEST}: pin (${pin}) is expected to be '${CLEVIS_PIN}'" |
| 115 | fi |
| 116 | |
| 117 | # Check configuration has "uri:" |
| 118 | if ! [[ "${cfg}" == *"uri"* ]]; then |
| 119 | error "${TEST}: configuration (${cfg}) is expected to be contain uri" |
| 120 | fi |
| 121 | |
| 122 | # Test the passphrase |
| 123 | SLT=1 |
| 124 | PASS=$(clevis luks pass -d "${DEV}" -s "${SLT}") |
| 125 | if ! clevis_luks_check_valid_key_or_keyfile "${DEV}" "${PASS}" "" "${SLT}"; then |
| 126 | error "Passphrase obtained from clevis luks pass failed." |
| 127 | fi |
| 128 | |
| 129 | if ! clevis luks unbind -f -d "${DEV}" -s "${SLT}"; then |
| 130 | error "${TEST}: Unbind is expected to succeed for device ${DEV} and slot ${SLT}" |
| 131 | fi |
| 132 | |
| 133 | SLT=0 |
| 134 | if clevis luks unbind -f -d "${DEV}" -s "${SLT}"; then |
| 135 | error "${TEST}: Unbind is expected to fail for device ${DEV}:${SLT} \ |
| 136 | that is not bound with clevis" |
| 137 | fi |
| 138 | |
| 139 | WRONGCFG=$(printf '{"uri": "pkcs12:"}') |
| 140 | if clevis luks bind -f -d "${DEV}" "${CLEVIS_PIN}" "${WRONGCFG}" <<< "${DEFAULT_PASS}"; \ |
| 141 | then |
| 142 | error "${TEST}: Binding is expected to fail when given an incorrect configuration:\ |
| 143 | (${WRONGCFG})" |
| 144 | fi |
| 145 | |
| 146 | WRONGCFG=$(printf '{"uri":""}') |
| 147 | if clevis luks bind -f -d "${DEV}" "${CLEVIS_PIN}" "${WRONGCFG}" <<< "${DEFAULT_PASS}"; \ |
| 148 | then |
| 149 | error "${TEST}: Binding is expected to fail when given an empty uri:\ |
| 150 | (${WRONGCFG})" |
| 151 | fi |
| 152 | |
| 153 | if command -v ps && command -v killall; then |
| 154 | if ! clevis_start_pcscd_server; |
| 155 | then |
| 156 | error "${TEST}: Could not start pcscd server" |
| 157 | fi |
| 158 | if ! killall -9 pcscd; |
| 159 | then |
| 160 | error "${TEST}: Could not kill pcscd server" |
| 161 | fi |
| 162 | fi |
| 163 | |
| 164 | softhsm_lib_cleanup |
| 165 | test "$?" == 0 |