tests-common-functions.in 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #!/bin/bash -ex
  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 <scorreia@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. . tang-common-test-functions
  21. error() {
  22. echo "${1}" >&2
  23. exit 1
  24. }
  25. skip_test() {
  26. echo "${1}" >&2
  27. exit 77
  28. }
  29. # We require cryptsetup >= 2.0.4 to fully support LUKSv2.
  30. # Support is determined at build time.
  31. luks2_supported() {
  32. return @OLD_CRYPTSETUP@
  33. }
  34. # Creates a new LUKS1 or LUKS2 device to be used.
  35. new_device() {
  36. local LUKS="${1}"
  37. local DEV="${2}"
  38. local PASS="${3}"
  39. # Some builders fail if the cryptsetup steps are not ran as root, so let's
  40. # skip the test now if not running as root.
  41. if [ "$(id -u)" != 0 ]; then
  42. skip_test "WARNING: You must be root to run this test; test skipped."
  43. fi
  44. # Using a default password, if none has been provided.
  45. if [ -z "${PASS}" ]; then
  46. PASS="${DEFAULT_PASS}"
  47. fi
  48. local DEV_CACHED="${TMP}/${LUKS}.cached"
  49. # Let's reuse an existing device, if there is one.
  50. if [ -f "${DEV_CACHED}" ]; then
  51. echo "Reusing cached ${LUKS} device..."
  52. cp -f "${DEV_CACHED}" "${DEV}"
  53. return 0
  54. fi
  55. fallocate -l64M "${DEV}"
  56. cryptsetup luksFormat --type "${LUKS}" --pbkdf pbkdf2 \
  57. --pbkdf-force-iterations 1000 --batch-mode \
  58. --force-password "${DEV}" <<< "${PASS}"
  59. # Caching the just-formatted device for possible reuse.
  60. cp -f "${DEV}" "${DEV_CACHED}"
  61. }
  62. # Creates a new LUKS1 or LUKS2 device to be used, using a keyfile.
  63. new_device_keyfile() {
  64. local LUKS="${1}"
  65. local DEV="${2}"
  66. local KEYFILE="${3}"
  67. # Some builders fail if the cryptsetup steps are not ran as root, so let's
  68. # skip the test now if not running as root.
  69. if [ "$(id -u)" != 0 ]; then
  70. skip_test "WARNING: You must be root to run this test; test skipped."
  71. fi
  72. if [[ -z "${KEYFILE}" ]] || [[ ! -f "${KEYFILE}" ]]; then
  73. error "Invalid keyfile (${KEYFILE})."
  74. fi
  75. fallocate -l64M "${DEV}"
  76. cryptsetup luksFormat --type "${LUKS}" --pbkdf pbkdf2 \
  77. --pbkdf-force-iterations 1000 --batch-mode \
  78. "${DEV}" "${KEYFILE}"
  79. }
  80. pin_cfg_equal() {
  81. # Let's remove the single quotes from the pin configuration.
  82. local cfg1="${1//\'/}"
  83. local cfg2="${2//\'/}"
  84. # Now we sort and present them in compact form.
  85. local sorted_cfg1 sorted_cfg2
  86. sorted_cfg1="$(jq --compact-output --sort-keys . < <(echo -n "${cfg1}"))"
  87. sorted_cfg2="$(jq --compact-output --sort-keys . < <(echo -n "${cfg2}"))"
  88. # And we finally compare.
  89. if [ "${sorted_cfg1}" = "${sorted_cfg2}" ]; then
  90. return 0
  91. fi
  92. return 1
  93. }
  94. compare_luks_header() {
  95. DEV1="${1}"
  96. DEV2="${2}"
  97. TMP="${3}"
  98. cryptsetup luksHeaderBackup "${DEV1}" \
  99. --header-backup-file "${TMP}"/check-header1
  100. cryptsetup luksHeaderBackup "${DEV2}" \
  101. --header-backup-file "${TMP}"/check-header2
  102. local cs1 cs2
  103. cs1=$(cksum "${TMP}"/check-header1 | cut -d' ' -f 1)
  104. cs2=$(cksum "${TMP}"/check-header2 | cut -d' ' -f 1)
  105. rm -f "${TMP}"/check-header{1,2}
  106. if [ "${cs1}" == "${cs2}" ]; then
  107. return 0
  108. fi
  109. return 1
  110. }
  111. used_luks1_metadata_slots() {
  112. DEV="${1}"
  113. if ! luksmeta test -d "${DEV}"; then
  114. echo ""
  115. return 0
  116. fi
  117. local clevis_uuid="cb6e8904-81ff-40da-a84a-07ab9ab5715e"
  118. luksmeta show -d "${DEV}" \
  119. | sed -rn "s|^([0-9]+)\s+active\s+${clevis_uuid}$|\1|p" \
  120. | tr '\n' ' ' | sed 's/ $//'
  121. }
  122. used_luks2_metadata_slots() {
  123. DEV="${1}"
  124. cryptsetup luksDump "${DEV}" \
  125. | grep -E -A1 "^\s+[0-9]+:\s+clevis$" \
  126. | sed -rn 's|^\s+Keyslot:\s+([0-9]+)$|\1|p' | sort -n \
  127. | tr '\n' ' ' | sed 's/ $//'
  128. }
  129. used_luks2_metadata_tokens() {
  130. DEV="${1}"
  131. cryptsetup luksDump "${DEV}" \
  132. | grep -E -B1 "^\s+Keyslot:\s+[0-9]+$" \
  133. | sed -rn 's|^\s+([0-9]+): clevis|\1|p' \
  134. | tr '\n' ' ' | sed 's/ $//'
  135. }
  136. compare_luks1_metadata() {
  137. DEV1="${1}"
  138. DEV2="${2}"
  139. # If both are non-initialized, metadata is the same.
  140. ! luksmeta test -d "${DEV1}" && ! luksmeta test -d "${DEV2}" && return 0
  141. # Otherwise, metadata differ.
  142. ! luksmeta test -d "${DEV1}" && return 1
  143. ! luksmeta test -d "${DEV2}" && return 1
  144. local slt1 slt2
  145. slt1=$(used_luks1_metadata_slots "${DEV1}")
  146. slt2=$(used_luks1_metadata_slots "${DEV2}")
  147. if [ "${slt1}" != "${slt2}" ]; then
  148. echo "used slots did not match ($slt1) ($slt2)" >&2
  149. return 1
  150. fi
  151. local slt md1 md2
  152. for slt in ${slt1}; do
  153. md1="$(luksmeta load -d "${DEV}" -s "${slt}")"
  154. md2="$(luksmeta load -d "${DEV2}" -s "${slt}")"
  155. if [ "${md1}" != "${md2}" ]; then
  156. echo "metadata in slot ${slt} did not match" >&2
  157. return 1
  158. fi
  159. done
  160. return 0
  161. }
  162. compare_luks2_metadata() {
  163. DEV1="${1}"
  164. DEV2="${2}"
  165. local slt1 slt2
  166. slt1=$(used_luks2_metadata_slots "${DEV1}")
  167. slt2=$(used_luks2_metadata_slots "${DEV2}")
  168. if [ "${slt1}" != "${slt2}" ]; then
  169. echo "used slots did not match ($slt1) ($slt2)" >&2
  170. return 1
  171. fi
  172. local tkn1 tkn2
  173. tkn1=$(used_luks2_metadata_tokens "${DEV1}")
  174. tkn2=$(used_luks2_metadata_tokens "${DEV2}")
  175. if [ "${tkn1}" != "${tkn2}" ]; then
  176. echo "used tokens did not match ($tkn1) ($tkn2)" >&2
  177. return 1
  178. fi
  179. local tkn md1 md2
  180. for tkn in ${tkn1}; do
  181. md1="$(cryptsetup token export --token-id "${tkn}" "${DEV1}")"
  182. md2="$(cryptsetup token export --token-id "${tkn}" "${DEV2}")"
  183. if [ "${md1}" != "${md2}" ]; then
  184. echo "metadata in token ${tkn} did not match" >&2
  185. return 1
  186. fi
  187. done
  188. return 0
  189. }
  190. new_passphrase() {
  191. jose jwk gen --input='{"kty":"oct","bytes":8}' --output=- \
  192. | jose fmt --json=- --object --get k --unquote=-
  193. }
  194. export DEFAULT_PASS=' just-some- test-password-here 1.+?~!@#$%^&*();:'"'"'"[]{}_=/`\ '