| 1 | #!/bin/bashscip-shell filesystem . . `/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-functionsscip-shell source . . tang-common-test-functions. |
| 23 | |
| 24 | TEST=$(basenamescip-shell system . . basename. "${0}") |
| 25 | |
| 26 | on_exitscip-shell shell clevis 23-1 on_exit().() { |
| 27 | exit_status=$? |
| 28 | tang_stop "${TMP}" |
| 29 | [scip-shell system . . `[`. -d "${TMP}" ] && rmscip-shell system . . rm. -rf "${TMP}" |
| 30 | exit "${exit_status}" |
| 31 | } |
| 32 | |
| 33 | trap 'on_exit' EXIT |
| 34 | |
| 35 | TMP="$(mktempscip-shell system . . mktemp. -d)" |
| 36 | |
| 37 | tang_run "${TMP}" |
| 38 | port=$(tang_get_port "${TMP}") |
| 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="$(printfscip-shell system . . printf. '%s' "${jwks}" | jose jwk use --input=- --required \ |
| 48 | --use deriveKey --output=-)" |
| 49 | |
| 50 | jose fmt --json="${enc}" --get keys --array \ |
| 51 | || enc="$(printfscip-shell system . . 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="$(printfscip-shell system . . 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="$(printfscip-shell system . . 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=$(printfscip-shell system . . printf. '%s%s' "${jwk}" "${data}" \ |
| 70 | | jose jwe enc --input="${jwe}" --key=- --detached=- --compact) |
| 71 | |
| 72 | if ! decoded="$(printfscip-shell system . . printf. '%s' "${encoded}" | clevis decrypt)"; then |
| 73 | error "${TEST}: decoding is expected to work (alg = ${alg})" |
| 74 | fi |
| 75 | |
| 76 | if [scip-shell system . . `[`. "${decoded}" != "${data}" ]; then |
| 77 | 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 | data="just another test" |
| 83 | for alg in ${CLEVIS_SUPPORTED_THP_ALGS}; do |
| 84 | thp="$(jose fmt --json="${adv}" -g payload -y -o- \ |
| 85 | | jose jwk use -i- -r -u verify -o- \ |
| 86 | | jose jwk thp -i- -a "${alg}")" |
| 87 | cfg="$(printfscip-shell system . . printf. '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")" |
| 88 | if ! encoded=$(printfscip-shell system . . printf. '%s' "${data}" | clevis encrypt tang "${cfg}"); then |
| 89 | error "${TEST}: tang encryption should have succeeded when providing the thp (${thp}) with any supported algorithm (${alg})" |
| 90 | fi |
| 91 | |
| 92 | if ! decoded="$(printfscip-shell system . . printf. '%s' "${encoded}" | clevis decrypt)"; then |
| 93 | error "${TEST}: decoding is expected to work (thp alg = ${alg})" |
| 94 | fi |
| 95 | |
| 96 | if [scip-shell system . . `[`. "${decoded}" != "${data}" ]; then |
| 97 | error "${TEST}: tang decrypt should have succeeded decoded[${decoded}] data[${data}] (alg = ${alg})" |
| 98 | fi |
| 99 | |
| 100 | done |
| 101 | |
| 102 | # Let's also try some unsupported thp hash algorithms. |
| 103 | UNSUPPORTED="S224 S384 S512" # SHA-224, SHA-384, SHA-512. |
| 104 | for alg in ${UNSUPPORTED}; do |
| 105 | thp="$(jose fmt --json="${adv}" -g payload -y -o- \ |
| 106 | | jose jwk use -i- -r -u verify -o- \ |
| 107 | | jose jwk thp -i- -a "${alg}")" |
| 108 | cfg="$(printfscip-shell system . . printf. '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")" |
| 109 | if echoscip-shell system . . echo. foo | clevis encrypt tang "${cfg}" >/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 110 | error "${TEST}: tang encryption should have failed when providing the thp (${thp}) with an unsupported algorithm (${alg})" |
| 111 | fi |
| 112 | done |
| 113 | |
| 114 | # Now let's try some bad values for thp. |
| 115 | for thp in "" "foo" "invalid"; do |
| 116 | cfg="$(printfscip-shell system . . printf. '{"url":"%s", "thp":"%s"}' "${url}" "${thp}")" |
| 117 | if echoscip-shell system . . echo. foo | clevis encrypt tang "${cfg}" >/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 118 | error "${TEST}: tang encryption expected to fail when providing a bad thp" |
| 119 | fi |
| 120 | done |