default-thp-alg 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/bin/bash
  2. set -exo pipefail
  3. # vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  4. #
  5. # Copyright (c) 2020 Red Hat, Inc.
  6. # Author: Sergio Correia <scorreia@redhat.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. . tang-common-test-functions
  21. TEST=$(basename "${0}")
  22. on_exit() {
  23. exit_status=$?
  24. tang_stop "${TMP}"
  25. [ -d "${TMP}" ] && rm -rf "${TMP}"
  26. exit "${exit_status}"
  27. }
  28. trap 'on_exit' EXIT
  29. TMP="$(mktemp -d)"
  30. port=$(tang_new_random_port)
  31. tang_run "${TMP}" "${port}"
  32. url="http://localhost:${port}"
  33. data="just a sample text"
  34. # Get the advertisement and extract the keys.
  35. adv="$(tang_get_adv "${port}")"
  36. jwks="$(jose fmt --json="${adv}" --get payload --b64load --output=-)"
  37. enc="$(printf '%s' "${jwks}" | jose jwk use --input=- --required \
  38. --use deriveKey --output=-)"
  39. jose fmt --json="${enc}" --get keys --array \
  40. || enc="$(printf '{"keys": [%s]}' "${enc}")"
  41. jwk="$(jose fmt --json="${enc}" --get keys --array --foreach=- \
  42. | jose fmt --json=- --delete key_ops --delete alg --output=-)"
  43. jwe_t='{"protected":{"alg":"ECDH-ES","enc":"A256GCM","clevis":{"pin":"tang","tang":{}}}}'
  44. jwe_t="$(jose fmt --json="${jwe_t}" --get protected --get clevis --get tang --quote "${url}" --set url -UUUUo-)"
  45. jwe_t="$(printf '%s' "${jwks}" | jose fmt --json="${jwe_t}" --get protected --get clevis --get tang --json=- --set adv -UUUUo-)"
  46. # We currently support SHA-1 (legacy) and SHA-256.
  47. CLEVIS_SUPPORTED_THP_ALGS="S1 S256"
  48. # Now we will use every hash algorithm supported by jose to create a thumbprint
  49. # for `kid', then we do the encoding and verify clevis decrypt can decode it
  50. # correctly.
  51. for alg in ${CLEVIS_SUPPORTED_THP_ALGS}; do
  52. kid="$(printf '%s' "${jwk}" | jose jwk thp -a "${alg}" --input=-)"
  53. jwe="$(jose fmt --json="${jwe_t}" --get protected --quote "${kid}" -s kid -UUo-)"
  54. encoded=$(printf '%s%s' "${jwk}" "${data}" \
  55. | jose jwe enc --input="${jwe}" --key=- --detached=- --compact)
  56. if ! decoded="$(printf '%s' "${encoded}" | clevis decrypt)"; then
  57. tang_error "${TEST}: decoding is expected to work (alg = ${alg})"
  58. fi
  59. if [ "${decoded}" != "${data}" ]; then
  60. tang_error "${TEST}: tang decrypt should have succeeded decoded[${decoded}] data[${data}] (alg = ${alg})"
  61. fi
  62. done
  63. # Now let's test encryption providing the thp in the configuration.
  64. data="just another test"
  65. for alg in ${CLEVIS_SUPPORTED_THP_ALGS}; do
  66. thp="$(jose fmt --json="${adv}" -g payload -y -o- \
  67. | jose jwk use -i- -r -u verify -o- \
  68. | jose jwk thp -i- -a "${alg}")"
  69. cfg="$(printf '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")"
  70. if ! encoded=$(printf '%s' "${data}" | clevis encrypt tang "${cfg}"); then
  71. tang_error "${TEST}: tang encryption should have succeeded when providing the thp (${thp}) with any supported algorithm (${alg})"
  72. fi
  73. if ! decoded="$(printf '%s' "${encoded}" | clevis decrypt)"; then
  74. tang_error "${TEST}: decoding is expected to work (thp alg = ${alg})"
  75. fi
  76. if [ "${decoded}" != "${data}" ]; then
  77. tang_error "${TEST}: tang decrypt should have succeeded decoded[${decoded}] data[${data}] (alg = ${alg})"
  78. fi
  79. done
  80. # Let's also try some unsupported thp hash algorithms.
  81. UNSUPPORTED="S224 S384 S512" # SHA-224, SHA-384, SHA-512.
  82. for alg in ${UNSUPPORTED}; do
  83. thp="$(jose fmt --json="${adv}" -g payload -y -o- \
  84. | jose jwk use -i- -r -u verify -o- \
  85. | jose jwk thp -i- -a "${alg}")"
  86. cfg="$(printf '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")"
  87. if echo foo | clevis encrypt tang "${cfg}" >/dev/null; then
  88. tang_error "${TEST}: tang encryption should have failed when providing the thp (${thp}) with an unsupported algorithm (${alg})"
  89. fi
  90. done
  91. # Now let's try some bad values for thp.
  92. for thp in "" "foo" "invalid"; do
  93. cfg="$(printf '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")"
  94. if echo foo | clevis encrypt tang "${cfg}" >/dev/null; then
  95. tang_error "${TEST}: tang encryption expected to fail when providing a bad thp"
  96. fi
  97. done