clevis-decrypt-tang 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 -a "$1" == "--summary" ] && exit 1
  21. if [ -t 0 ]; then
  22. echo >&2
  23. echo "Usage: clevis decrypt tang < JWE > PLAINTEXT" >&2
  24. echo >&2
  25. exit 1
  26. fi
  27. read -d . hdr
  28. if ! jhd=`jose b64 dec -i- <<< "$hdr"`; then
  29. echo "Error decoding JWE protected header!" >&2
  30. exit 1
  31. fi
  32. if [ "`jose fmt -j- -Og clevis -g pin -u- <<< "$jhd"`" != "tang" ]; then
  33. echo "JWE pin mismatch!" >&2
  34. exit 1
  35. fi
  36. if ! clt=`jose fmt -j- -Og epk -Oo- <<< "$jhd"`; then
  37. echo "JWE missing required 'epk' header parameter!" >&2
  38. exit 1
  39. fi
  40. if ! kid=`jose fmt -j- -Og kid -Su- <<< "$jhd"`; then
  41. echo "JWE missing required 'kid' header parameter!" >&2
  42. exit 1
  43. fi
  44. if ! srv=`jose fmt -j- -Og clevis -g tang -g adv -Oo- <<< "$jhd" \
  45. | jose jwk thp -i- -f "$kid"`; then
  46. echo "JWE missing required 'clevis.tang.adv' header parameter!" >&2
  47. exit 1
  48. fi
  49. if ! url=`jose fmt -j- -Og clevis -g tang -g url -Su- <<< "$jhd"`; then
  50. echo "JWE missing required 'clevis.tang.url' header parameter!" >&2
  51. exit 1
  52. fi
  53. if ! crv=`jose fmt -j- -Og crv -Su- <<< "$clt"`; then
  54. echo "Unable to determine EPK's curve!" >&2
  55. exit 1
  56. fi
  57. if ! eph=`jose jwk gen -i "{\"alg\":\"ECMR\",\"crv\":\"$crv\"}"`; then
  58. echo "Error generating ephemeral key!" >&2
  59. exit 1
  60. fi
  61. xfr=`jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$clt$eph"`
  62. url="$url/rec/$kid"
  63. ct="Content-Type: application/jwk+json"
  64. if ! rep=`curl -sfg -X POST -H "$ct" --data-binary @- "$url" <<< "$xfr"`; then
  65. echo "Error communicating with the server!" >&2
  66. exit 1
  67. fi
  68. if ! rep=`jose fmt -j- -Og kty -q EC -EUUg crv -q "$crv" -EUUo- <<< "$rep"`; then
  69. echo "Received invalid server reply!" >&2
  70. exit 1
  71. fi
  72. tmp=`jose jwk exc -i '{"alg":"ECMR"}' -l- -r- <<< "$eph$srv"`
  73. rep=`jose jwk pub -i- <<< "$rep"`
  74. jwk=`jose jwk exc -l- -r- <<< "$rep$tmp"`
  75. exec jose jwe dec -k- -i- < <(echo -n "$jwk$hdr."; cat)