clevis-decrypt 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. SUMMARY="Decrypts using the policy defined at encryption time"
  21. function findexe() {
  22. [ $# -eq 1 ] || return 1
  23. while read -r -d: path; do
  24. [ -f "$path/$1" ] && [ -x "$path/$1" ] && echo "$path/$1" && return 0
  25. done <<< "$PATH:"
  26. return 1
  27. }
  28. if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
  29. echo "$SUMMARY"
  30. exit 0
  31. fi
  32. if ! [ -t 0 ]; then
  33. read -r -d . hdr
  34. if ! pin="$(jose fmt -q "$hdr" -SyOg clevis -Og pin -Su-)"; then
  35. echo "JWE is missing the required 'clevis.pin' header property!" >&2
  36. exit 1
  37. fi
  38. if ! cmd="$(findexe clevis-decrypt-"$pin")"; then
  39. echo "Unable to locate pin '$pin'!" >&2
  40. exit 1
  41. fi
  42. (echo -n "$hdr."; /bin/cat) | "$cmd"
  43. exit $?
  44. fi
  45. exec >&2
  46. echo
  47. echo "Usage: clevis decrypt < JWE > PLAINTEXT"
  48. echo
  49. echo "$SUMMARY"
  50. echo
  51. exit 2