bind-luks2-ext-token 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash -ex
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2022 Red Hat, Inc.
  5. # Author: Sergio Arroutbi <sarroutb@redhat.com>
  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. TEST=$(basename "${0}")
  21. . tests-common-functions
  22. on_exit() {
  23. [ -d "${TMP}" ] && rm -rf "${TMP}"
  24. }
  25. create_existing_token_id_from_keyring() {
  26. local DEV="${1}"
  27. local KEYDESC="${2}"
  28. local TOKEN_ID="${3}"
  29. local PASS="${4}"
  30. if [[ -z "${DEV}" ]] || [[ -z "${KEYDESC}" ]] || [[ -z "${TOKEN_ID}" ]]; then
  31. return 1
  32. fi
  33. KEYRING_ID=$(keyctl add user "${KEYDESC}" "${PASS}" @s)
  34. keyctl print "${KEYRING_ID}" 2>/dev/null 1>/dev/null
  35. cryptsetup token add --token-id "${TOKEN_ID}" --key-description "${KEYDESC}" "${DEV}"
  36. }
  37. if ! luks2_supported; then
  38. skip_test "${TEST}: LUKS2 is not supported."
  39. fi
  40. if ! luks2_existing_token_id_supported; then
  41. skip_test "${TEST}: Existing token ID not supported"
  42. fi
  43. trap 'on_exit' EXIT
  44. trap 'exit' ERR
  45. TMP="$(mktemp -d)"
  46. ADV="${TMP}/adv.jws"
  47. tang_create_adv "${TMP}" "${ADV}"
  48. CFG="$(printf '{"url":"foobar","adv":"%s"}' "$ADV")"
  49. EXISTING_TOKEN_ID=5
  50. KEYDESC="testkey"
  51. PASS="123exttokenid_"
  52. DEV="${TMP}/luks2-device-ext-token"
  53. new_device "luks2" "${DEV}" "${PASS}"
  54. create_existing_token_id_from_keyring "${DEV}" "${KEYDESC}" "${EXISTING_TOKEN_ID}" "${PASS}"
  55. if ! clevis luks bind -y -d "${DEV}" -e "${EXISTING_TOKEN_ID}" tang "${CFG}"; then
  56. error "${TEST}: Binding expected to succeed with existing token id:${EXISTING_TOKEN_ID}" >&2
  57. fi
  58. KEYFILE="${TMP}/keyfile.txt"
  59. touch "${KEYFILE}"
  60. if clevis luks bind -y -d "${DEV}" -e "${EXISTING_TOKEN_ID}" -k "${KEYFILE}" tang "${CFG}"; then
  61. error "${TEST}: Using existing token id and keyfile should dump an error" >&2
  62. fi