tangd-rotate-keys 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. local _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. local _msg="${1}"
  38. local _verbose="${2:-}"
  39. [ -z "${_verbose}" ] && return 0
  40. echo "${_msg}" >&2
  41. }
  42. error() {
  43. log "${1}" 1
  44. usage 1
  45. }
  46. JWKDIR=
  47. VERBOSE=
  48. while getopts "hvd:" o; do
  49. case "${o}" in
  50. d) JWKDIR="${OPTARG}";;
  51. h) usage 0;;
  52. v) VERBOSE=1;;
  53. *) usage 1;;
  54. esac
  55. done
  56. [ -z "${JWKDIR}" ] && error "Please specify the keys directory with -d switch"
  57. [ -r "${JWKDIR}" ] || error "Error trying to access JWK directory '${JWKDIR}'"
  58. cd "${JWKDIR}" || error "Unable to change to keys directory '${JWKDIR}'"
  59. # Disable advertisement of current keys.
  60. for key in *.jwk; do
  61. [ -r "${key}" ] || continue
  62. mv -f -- "${key}" ."${key}"
  63. log "Disabled advertisement of key ${key} -> .${key}" "${VERBOSE}"
  64. done
  65. # Create a new set of keys.
  66. DEFAULT_THP_HASH="S256"
  67. for alg in "ES512" "ECMR"; do
  68. json="$(printf '{"alg": "%s"}' "${alg}")"
  69. jwe="$(jose jwk gen --input "${json}")"
  70. thp="$(printf '%s' "${jwe}" | jose jwk thp --input=- \
  71. -a "${DEFAULT_THP_HASH}")"
  72. echo "${jwe}" > "${thp}.jwk"
  73. log "Created new key ${thp}.jwk" "${VERBOSE}"
  74. done
  75. cd - >/dev/null
  76. log "Keys rotated successfully" "${VERBOSE}"