clevis-encrypt-tang 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 Tang binding server policy"
  21. if [ "$1" == "--summary" ]; then
  22. echo "$SUMMARY"
  23. exit 0
  24. fi
  25. if [ -t 0 ]; then
  26. echo >&2
  27. echo "Usage: clevis encrypt tang CONFIG < PLAINTEXT > JWE" >&2
  28. echo >&2
  29. echo $SUMMARY >&2
  30. echo >&2
  31. echo "This command uses the following configuration properties:" >&2
  32. echo >&2
  33. echo " url: <string> The base URL of the Tang server (REQUIRED)" >&2
  34. echo >&2
  35. echo " thp: <string> The thumbprint of a trusted signing key" >&2
  36. echo >&2
  37. echo " adv: <string> A filename containing a trusted advertisement" >&2
  38. echo " adv: <object> A trusted advertisement (raw JSON)" >&2
  39. echo >&2
  40. echo "Obtaining the thumbprint of a trusted signing key is easy. If you" >&2
  41. echo "have access to the Tang server's database directory, simply do:" >&2
  42. echo >&2
  43. echo " $ jose jwk thp -i \$DBDIR/\$SIG.jwk " >&2
  44. echo >&2
  45. echo "Alternatively, if you have certainty that your network connection" >&2
  46. echo "is not compromised (not likely), you can download the advertisement" >&2
  47. echo "yourself using:" >&2
  48. echo >&2
  49. echo " $ curl -f \$URL/adv > adv.jws" >&2
  50. echo >&2
  51. exit 1
  52. fi
  53. if ! cfg=`jose fmt -j- -Oo- <<< "$1" 2>/dev/null`; then
  54. echo "Configuration is malformed!" >&2
  55. exit 1
  56. fi
  57. if ! url=`jose fmt -j- -Og url -u- <<< "$cfg"`; then
  58. echo "Missing the required 'url' property!" >&2
  59. exit 1
  60. fi
  61. thp=`jose fmt -j- -Og thp -Su- <<< "$cfg"` || true
  62. ### Get the advertisement
  63. if jws=`jose fmt -j- -g adv -Oo- <<< "$cfg"`; then
  64. thp=${thp:-any}
  65. elif jws=`jose fmt -j- -g adv -Su- <<< "$cfg"`; then
  66. if ! [ -f "$jws" ]; then
  67. echo "Advertisement file '$jws' not found!" >&2
  68. exit 1
  69. fi
  70. if ! jws=`jose fmt -j- -Oo- < "$jws"`; then
  71. echo "Advertisement file '$jws' is malformed!" >&2
  72. exit 1
  73. fi
  74. thp=${thp:-any}
  75. elif ! jws=`curl -sfg "$url/adv/$thp"`; then
  76. echo "Unable to fetch advertisement: '$url/adv/$thp'!" >&2
  77. exit 1
  78. fi
  79. if ! jwks=`jose fmt -j- -Og payload -SyOg keys -AUo- <<< "$jws"`; then
  80. echo "Advertisement is malformed!" >&2
  81. exit 1
  82. fi
  83. ### Check advertisement validity
  84. ver=`jose jwk use -i- -r -u verify -o- <<< "$jwks"`
  85. if ! jose jws ver -i "$jws" -k- -a <<< "$ver"; then
  86. echo "Advertisement is missing signatures!" >&2
  87. exit 1
  88. fi
  89. ### Check advertisement trust
  90. if [ -z "$thp" ]; then
  91. echo "The advertisement contains the following signing keys:" >&2
  92. echo >&2
  93. jose jwk thp -i- <<< "$ver" >&2
  94. echo >&2
  95. read -r -p "Do you wish to trust these keys? [ynYN] " ans < /dev/tty
  96. [[ "$ans" =~ ^[yY]$ ]] || exit 1
  97. elif [ "$thp" != "any" ] && \
  98. ! jose jwk thp -i- -f "$thp" -o /dev/null <<< "$ver"; then
  99. echo "Trusted JWK '$thp' did not sign the advertisement!" >&2
  100. exit 1
  101. fi
  102. ### Perform encryption
  103. enc=`jose jwk use -i- -r -u deriveKey -o- <<< "$jwks"`
  104. jose fmt -j "$enc" -Og keys -A || enc="{\"keys\":[$enc]}"
  105. for jwk in `jose fmt -j- -Og keys -Af- <<< "$enc"`; do
  106. jwk=`jose fmt -j- -Od key_ops -o- <<< "$jwk"`
  107. jwk=`jose fmt -j- -Od alg -o- <<< "$jwk"`
  108. kid=`jose jwk thp -i- <<< "$jwk"`
  109. jwe='{"protected":{"alg":"ECDH-ES","enc":"A256GCM","clevis":{"pin":"tang","tang":{}}}}'
  110. jwe=`jose fmt -j "$jwe" -g protected -q "$kid" -s kid -UUo-`
  111. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tang -q "$url" -s url -UUUUo-`
  112. jwe=`jose fmt -j "$jwe" -g protected -g clevis -g tang -j- -s adv -UUUUo- <<< "$jwks"`
  113. exec jose jwe enc -i- -k- -I- -c < <(echo -n "$jwe$jwk"; cat)
  114. done
  115. echo "No exchange keys found!" >&2
  116. exit 1