clevis-decrypt-http 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 http < JWE > PLAINTEXT" >&2
  24. echo >&2
  25. exit 1
  26. fi
  27. read -d . hdr
  28. if [ "`jose fmt -q "$hdr" -SyOg clevis -g pin -u-`" != "http" ]; then
  29. echo "JWE pin mismatch!" >&2
  30. exit 1
  31. fi
  32. if ! url=`jose fmt -q "$hdr" -SyOg clevis -g http -g url -u-`; then
  33. echo "JWE missing 'clevis.http.url' header parameter!" >&2
  34. exit 1
  35. fi
  36. if ! typ=`jose fmt -q "$hdr" -SyOg clevis -g http -g type -u-`; then
  37. echo "JWE missing 'clevis.http.url' header parameter!" >&2
  38. exit 1
  39. fi
  40. if ! rep=`curl -sfg -H "Accept: $typ" "$url"`; then
  41. echo "Key transfer failed!" >&2
  42. exit 1
  43. fi
  44. case $typ in
  45. application/jwk+json)
  46. if ! rep=`curl -sfg -H "Accept: $typ" "$url" \
  47. | jose fmt -j- -Og kty -q oct -EUUo-`; then
  48. echo "Key transfer failed!" >&2
  49. exit 1
  50. fi
  51. ;;
  52. application/octet-stream)
  53. if ! key=`curl -sfg -H "Accept: $typ" "$url" | jose b64 enc -I-`; then
  54. echo "Key transfer failed!" >&2
  55. exit 1
  56. fi
  57. jwk="{\"kty\":\"oct\",\"k\":\"$key\"}"
  58. ;;
  59. esac
  60. exec jose jwe dec -k- -i- < <(echo -n "$jwk$hdr."; cat)