clevis-decrypt-tang 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2017 Red Hat, Inc.
  5. # Author: Nathaniel McCallum <npmccallum@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. [ $# -eq 1 ] && [ "$1" == "--summary" ] && exit 2
  21. if [ -t 0 ]; then
  22. exec >&2
  23. echo
  24. echo "Usage: clevis decrypt tang < JWE > PLAINTEXT"
  25. echo
  26. exit 2
  27. fi
  28. read -r -d . hdr
  29. if ! jhd="$(jose b64 dec -i- <<< "$hdr")"; then
  30. echo "Error decoding JWE protected header!" >&2
  31. exit 1
  32. fi
  33. if [ "$(jose fmt -j- -Og clevis -g pin -u- <<< "$jhd")" != "tang" ]; then
  34. echo "JWE pin mismatch!" >&2
  35. exit 1
  36. fi
  37. if ! clt="$(jose fmt -j- -Og epk -Oo- <<< "$jhd")"; then
  38. echo "JWE missing required 'epk' header parameter!" >&2
  39. exit 1
  40. fi
  41. if ! kid="$(jose fmt -j- -Og kid -Su- <<< "$jhd")"; then
  42. echo "JWE missing required 'kid' header parameter!" >&2
  43. exit 1
  44. fi
  45. # Tang advertisement validation.
  46. if ! keys="$(jose fmt -j- -Og clevis -g tang -g adv -Oo- <<< "${jhd}")"; then
  47. echo "JWE missing required 'clevis.tang.adv' header parameter!" >&2
  48. exit 1
  49. fi
  50. # Check if the thumbprint we have in `kid' is in the advertised keys.
  51. CLEVIS_DEFAULT_THP_ALG=S256 # SHA-256.
  52. CLEVIS_DEFAULT_THP_LEN=43 # Length of SHA-256 thumbprint.
  53. CLEVIS_ALTERNATIVE_THP_ALGS=S1 # SHA-1.
  54. # Issue a warning if we are using a hash that has a shorter length than the
  55. # default one.
  56. if [ "${#kid}" -lt "${CLEVIS_DEFAULT_THP_LEN}" ]; then
  57. echo "WARNING: tang using a deprecated hash for the JWK thumbprints" >&2
  58. fi
  59. if ! srv="$(jose jwk thp -i- -f "${kid}" -a "${CLEVIS_DEFAULT_THP_ALG}" \
  60. <<< "${keys}")"; then
  61. # `kid' thumprint not in the advertised keys, but it's possible it was
  62. # generated using a different algorithm than the default one.
  63. # Let us try the alternative supported algorithms to make sure `kid'
  64. # really is not part of the advertised keys.
  65. for alg in ${CLEVIS_ALTERNATIVE_THP_ALGS}; do
  66. srv="$(jose jwk thp -i- -f "$kid" -a "${alg}" <<< "${keys}")" && break
  67. done
  68. if [ -z "${srv}" ]; then
  69. echo "JWE header validation of 'clevis.tang.adv' failed: key thumbprint does not match" >&2
  70. exit 1
  71. fi
  72. fi
  73. if ! url="$(jose fmt -j- -Og clevis -g tang -g url -Su- <<< "$jhd")"; then
  74. echo "JWE missing required 'clevis.tang.url' header parameter!" >&2
  75. exit 1
  76. fi
  77. if ! crv="$(jose fmt -j- -Og crv -Su- <<< "$clt")"; then
  78. echo "Unable to determine EPK's curve!" >&2
  79. exit 1
  80. fi
  81. if ! eph="$(jose jwk gen -i "{\"alg\":\"ECMR\",\"crv\":\"$crv\"}")"; then
  82. echo "Error generating ephemeral key!" >&2
  83. exit 1
  84. fi
  85. xfr="$(jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$clt$eph")"
  86. url="$url/rec/$kid"
  87. ct="Content-Type: application/jwk+json"
  88. if ! rep="$(curl -sfg -X POST -H "$ct" --data-binary @- "$url" <<< "$xfr")"; then
  89. echo "Error communicating with the server!" >&2
  90. exit 1
  91. fi
  92. if ! rep="$(jose fmt -j- -Og kty -q EC -EUUg crv -q "$crv" -EUUo- <<< "$rep")"; then
  93. echo "Received invalid server reply!" >&2
  94. exit 1
  95. fi
  96. tmp="$(jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$eph$srv")"
  97. rep="$(jose jwk pub -i- <<< "$rep")"
  98. jwk="$(jose jwk exc -l- -r- <<< "$rep$tmp")"
  99. (echo -n "$jwk$hdr."; /bin/cat) | jose jwe dec -k- -i-
  100. exit $?