clevis-decrypt 1.6 KB

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