clevis-encrypt-http 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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="Encrypts using a REST HTTP escrow server policy"
  21. function http() {
  22. curl -sfg -X "$1" -H "Content-Type: $2" --data-binary @- "$3"
  23. }
  24. if [ "$1" == "--summary" ]; then
  25. echo "$SUMMARY"
  26. exit 0
  27. fi
  28. if [ -t 0 ]; then
  29. echo >&2
  30. echo "Usage: clevis encrypt http CONFIG < PLAINTEXT > JWE" >&2
  31. echo >&2
  32. echo "$SUMMARY" >&2
  33. echo >&2
  34. echo "This command uses the following configuration properties:" >&2
  35. echo >&2
  36. echo " url: <string> The URL where the key is stored (REQUIRED)" >&2
  37. echo >&2
  38. echo " http: <boolean> Allow or disallow non-TLS HTTP (default: false)" >&2
  39. echo >&2
  40. echo " type: <string> The type of key to store (default: octet-stream)" >&2
  41. echo >&2
  42. echo " method: <string> The HTTP method to use (default: PUT)" >&2
  43. echo >&2
  44. exit 1
  45. fi
  46. if ! cfg=`jose fmt -j "$1" -Oo- 2>/dev/null`; then
  47. echo "Configuration is malformed!" >&2
  48. exit 1
  49. fi
  50. if ! url=`jose fmt -j "$cfg" -g url -u-`; then
  51. echo "Configuration is missing required 'url' property!" >&2
  52. exit 1
  53. fi
  54. case $url in
  55. http:*)
  56. if ! jose fmt -j "$cfg" -g http -T; then
  57. echo "HTTP is not allowed (see 'http' config property)!" >&2
  58. exit 1
  59. fi ;;
  60. https:*) ;;
  61. *) echo "URL '$url' not supported!" >&2; exit 1;;
  62. esac
  63. typ=`jose fmt -j "$cfg" -Og type -u-` || typ="octet-stream"
  64. case $typ in
  65. jwk+json) typ="application/jwk+json" ;;
  66. octet-stream) typ="application/octet-stream" ;;
  67. application/jwk+json) ;;
  68. application/octet-stream) ;;
  69. *) echo "Type '$typ' not supported!" >&2; exit 1;;
  70. esac
  71. mth=`jose fmt -j "$cfg" -Og method -u-` || mth=PUT
  72. case $mth in
  73. PUT) ;;
  74. POST) ;;
  75. *) echo "Method '$mth' not supported!" >&2; exit 1;;
  76. esac
  77. jwk=`jose jwk gen -i '{"alg":"A256GCM"}'`
  78. jwe='{"protected":{"clevis":{"pin":"http","http":{}}}}'
  79. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g http -q "$url" -s url -UUUUo-`
  80. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g http -q "$typ" -s type -UUUUo-`
  81. case $typ in
  82. application/jwk+json)
  83. if ! http "$mth" "$typ" "$url" <<< "$jwk"; then
  84. echo "Key transfer failed!" >&2
  85. exit 1
  86. fi
  87. ;;
  88. application/octet-stream)
  89. if ! jose fmt -j- -g k -u- <<< "$jwk" | jose b64 dec -i- | http "$mth" "$typ" "$url"; then
  90. echo "Key transfer failed!" >&2
  91. exit 1
  92. fi
  93. ;;
  94. esac
  95. exec jose jwe enc -i "$jwe" -k- -I- -c < <(echo -n "$jwk"; cat)