|
| 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 <[email protected]> |
| 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 | + |
| 21 | + |
| 22 | +. tang-common-test-functions |
| 23 | + |
| 24 | +TEST=$(basename "${0}") |
| 25 | + |
| 26 | +on_exit() { |
| 27 | + exit_status=$? |
| 28 | + tang_stop "${TMP}" |
| 29 | + [ -d "${TMP}" ] && rm -rf "${TMP}" |
| 30 | + exit "${exit_status}" |
| 31 | +} |
| 32 | + |
| 33 | +trap 'on_exit' EXIT |
| 34 | + |
| 35 | +TMP="$(mktemp -d)" |
| 36 | + |
| 37 | +port=$(tang_new_random_port) |
| 38 | +tang_run "${TMP}" "${port}" |
| 39 | + |
| 40 | +url="http://localhost:${port}" |
| 41 | +data="just a sample text" |
| 42 | + |
| 43 | +# Get the advertisement and extract the keys. |
| 44 | +adv="$(tang_get_adv "${port}")" |
| 45 | + |
| 46 | +jwks="$(jose fmt --json="${adv}" --get payload --b64load --output=-)" |
| 47 | +enc="$(printf '%s' "${jwks}" | jose jwk use --input=- --required \ |
| 48 | + --use deriveKey --output=-)" |
| 49 | + |
| 50 | +jose fmt --json="${enc}" --get keys --array \ |
| 51 | + || enc="$(printf '{"keys": [%s]}' "${enc}")" |
| 52 | + |
| 53 | +jwk="$(jose fmt --json="${enc}" --get keys --array --foreach=- \ |
| 54 | + | jose fmt --json=- --delete key_ops --delete alg --output=-)" |
| 55 | + |
| 56 | +jwe_t='{"protected":{"alg":"ECDH-ES","enc":"A256GCM","clevis":{"pin":"tang","tang":{}}}}' |
| 57 | +jwe_t="$(jose fmt --json="${jwe_t}" --get protected --get clevis --get tang --quote "${url}" --set url -UUUUo-)" |
| 58 | +jwe_t="$(printf '%s' "${jwks}" | jose fmt --json="${jwe_t}" --get protected --get clevis --get tang --json=- --set adv -UUUUo-)" |
| 59 | + |
| 60 | +# We currently support SHA-1 (legacy) and SHA-256. |
| 61 | +CLEVIS_SUPPORTED_THP_ALGS="S1 S256" |
| 62 | +# Now we will use every hash algorithm supported by jose to create a thumbprint |
| 63 | +# for `kid', then we do the encoding and verify clevis decrypt can decode it |
| 64 | +# correctly. |
| 65 | +for alg in ${CLEVIS_SUPPORTED_THP_ALGS}; do |
| 66 | + kid="$(printf '%s' "${jwk}" | jose jwk thp -a "${alg}" --input=-)" |
| 67 | + jwe="$(jose fmt --json="${jwe_t}" --get protected --quote "${kid}" -s kid -UUo-)" |
| 68 | + |
| 69 | + encoded=$(printf '%s%s' "${jwk}" "${data}" \ |
| 70 | + | jose jwe enc --input="${jwe}" --key=- --detached=- --compact) |
| 71 | + |
| 72 | + if ! decoded="$(printf '%s' "${encoded}" | clevis decrypt)"; then |
| 73 | + tang_error "${TEST}: decoding is expected to work (alg = ${alg})" |
| 74 | + fi |
| 75 | + |
| 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 | + |
| 81 | +# Now let's test encryption providing the thp in the configuration. |
| 82 | +for alg in ${CLEVIS_SUPPORTED_THP_ALGS}; 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 succeeded when providing the thp (${thp}) with any supported algorithm (${alg})" |
| 89 | + fi |
| 90 | +done |
| 91 | + |
| 92 | +# Let's also try some unsupported thp hash algorithms. |
| 93 | +UNSUPPORTED="S224 S384 S512" # SHA-224, SHA-384, SHA-512. |
| 94 | +for alg in ${UNSUPPORTED}; do |
| 95 | + thp="$(jose fmt --json="${adv}" -g payload -y -o- \ |
| 96 | + | jose jwk use -i- -r -u verify -o- \ |
| 97 | + | jose jwk thp -i- -a "${alg}")" |
| 98 | + cfg="$(printf '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")" |
| 99 | + if echo foo | clevis encrypt tang "${cfg}" >/dev/null; then |
| 100 | + tang_error "${TEST}: tang encryption should have failed when providing the thp (${thp}) with an unsupported algorithm (${alg})" |
| 101 | + fi |
| 102 | +done |
| 103 | + |
| 104 | +# Now let's try some bad values for thp. |
| 105 | +for thp in "" "foo" "invalid"; do |
| 106 | + cfg="$(printf '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")" |
| 107 | + if echo foo | clevis encrypt tang "${cfg}" >/dev/null; then |
| 108 | + tang_error "${TEST}: tang encryption expected to fail when providing a bad thp" |
| 109 | + fi |
| 110 | +done |
0 commit comments