clevis-encrypt-tang 5.3 KB

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