tangd-rotate-keys.in 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh -e
  2. # vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2020 Red Hat, Inc.
  5. # Author: Sergio Correia <scorreia@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="Perform rotation of tang keys"
  21. usage() {
  22. _ret="${1:-1}"
  23. exec >&2
  24. echo "Usage: ${0} [-h] [-v] -d <KEYDIR>"
  25. echo
  26. echo "${SUMMARY}"
  27. echo
  28. echo " -d KEYDIR The directory with the keys, e.g. /var/db/tang"
  29. echo
  30. echo " -h Display this usage information"
  31. echo
  32. echo " -v Verbose. Display additional info on keys created/rotated"
  33. echo
  34. exit "${_ret}"
  35. }
  36. log() {
  37. _msg="${1}"
  38. _verbose="${2:-}"
  39. [ -z "${_verbose}" ] && return 0
  40. echo "${_msg}" >&2
  41. }
  42. error() {
  43. log "${1}" 1
  44. usage 1
  45. }
  46. set_perms() {
  47. chmod -- 0440 "${1}"
  48. if ! chown -- @user@:@group@ "${1}" 2>/dev/null; then
  49. echo "Unable to change owner/group for ${1} to @user@:@group@" >&2
  50. fi
  51. }
  52. JWKDIR=
  53. VERBOSE=
  54. while getopts "hvd:" o; do
  55. case "${o}" in
  56. d) JWKDIR="${OPTARG}";;
  57. h) usage 0;;
  58. v) VERBOSE=1;;
  59. *) usage 1;;
  60. esac
  61. done
  62. [ -z "${JWKDIR}" ] && error "Please specify the keys directory with -d switch"
  63. [ -r "${JWKDIR}" ] || error "Error trying to access JWK directory '${JWKDIR}'"
  64. cd "${JWKDIR}" || error "Unable to change to keys directory '${JWKDIR}'"
  65. # Disable advertisement of current keys.
  66. for key in *.jwk; do
  67. [ -r "${key}" ] || continue
  68. mv -f -- "${key}" ."${key}"
  69. log "Disabled advertisement of key ${key} -> .${key}" "${VERBOSE}"
  70. done
  71. # Create a new set of keys.
  72. DEFAULT_THP_HASH="S256"
  73. for alg in "ES512" "ECMR"; do
  74. json="$(printf '{"alg": "%s"}' "${alg}")"
  75. jwe="$(jose jwk gen --input "${json}")"
  76. thp="$(printf '%s' "${jwe}" | jose jwk thp --input=- \
  77. -a "${DEFAULT_THP_HASH}")"
  78. echo "${jwe}" > "${thp}.jwk"
  79. set_perms "${thp}.jwk"
  80. log "Created new key ${thp}.jwk" "${VERBOSE}"
  81. done
  82. cd - >/dev/null
  83. log "Keys rotated successfully" "${VERBOSE}"