clevis-encrypt-tang 4.4 KB

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