clevis-decrypt-tang 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if ! srv="$(jose fmt -j- -Og clevis -g tang -g adv -Oo- <<< "$jhd" \
  46. | jose jwk thp -i- -f "$kid")"; then
  47. echo "JWE missing required 'clevis.tang.adv' header parameter!" >&2
  48. exit 1
  49. fi
  50. if ! url="$(jose fmt -j- -Og clevis -g tang -g url -Su- <<< "$jhd")"; then
  51. echo "JWE missing required 'clevis.tang.url' header parameter!" >&2
  52. exit 1
  53. fi
  54. if ! crv="$(jose fmt -j- -Og crv -Su- <<< "$clt")"; then
  55. echo "Unable to determine EPK's curve!" >&2
  56. exit 1
  57. fi
  58. if ! eph="$(jose jwk gen -i "{\"alg\":\"ECMR\",\"crv\":\"$crv\"}")"; then
  59. echo "Error generating ephemeral key!" >&2
  60. exit 1
  61. fi
  62. xfr="$(jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$clt$eph")"
  63. url="$url/rec/$kid"
  64. ct="Content-Type: application/jwk+json"
  65. if ! rep="$(curl -sfg -X POST -H "$ct" --data-binary @- "$url" <<< "$xfr")"; then
  66. echo "Error communicating with the server!" >&2
  67. exit 1
  68. fi
  69. if ! rep="$(jose fmt -j- -Og kty -q EC -EUUg crv -q "$crv" -EUUo- <<< "$rep")"; then
  70. echo "Received invalid server reply!" >&2
  71. exit 1
  72. fi
  73. tmp="$(jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$eph$srv")"
  74. rep="$(jose jwk pub -i- <<< "$rep")"
  75. jwk="$(jose jwk exc -l- -r- <<< "$rep$tmp")"
  76. (echo -n "$jwk$hdr."; /bin/cat) | jose jwe dec -k- -i-
  77. exit $?