1606661229.v9-5-gfd69796.add-tangd-rotate-keys-helper-script.patch 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Subject: Add tangd-rotate-keys helper script
  2. Origin: v9-5-gfd69796 <https://github.com/latchset/tang/commit/v9-5-gfd69796>
  3. Upstream-Author: Sergio Correia <scorreia@redhat.com>
  4. Date: Sun Nov 29 11:47:09 2020 -0300
  5. So that it becomes simpler to perform key rotation on the server side.
  6. Usage: tangd-rotate-keys [-h] [-v] -d <KEYDIR>
  7. Example:
  8. $ sudo tangd-rotate-keys -d /var/db/tang -v
  9. Disabled advertisement of key 5AiUA4IhvOFdXzFavO78TKJ8hEsfGk8I6ymy4rBPWi8.jwk -> .5AiUA4IhvOFdXzFavO78TKJ8hEsfGk8I6ymy4rBPWi8.jwk
  10. Disabled advertisement of key dDC74X-o31Fq5VJaM9iZ4baZD2hhHw-RrIMkxEz35Xc.jwk -> .dDC74X-o31Fq5VJaM9iZ4baZD2hhHw-RrIMkxEz35Xc.jwk
  11. Created new key bIGVyIP2D_NJGQeFA9cf9oix5KEVQyVq9ZGjjv0s3D8.jwk
  12. Created new key BL4IR73UhG8yyYbvGJspPIlLvG6AzTnM850tlCKrcII.jwk
  13. Keys rotated successfully
  14. --- a/src/meson.build
  15. +++ b/src/meson.build
  16. @@ -9,5 +9,6 @@
  17. bins += join_paths(meson.current_source_dir(), 'tang-show-keys')
  18. libexecbins += join_paths(meson.current_source_dir(), 'tangd-keygen')
  19. +libexecbins += join_paths(meson.current_source_dir(), 'tangd-rotate-keys')
  20. # vim:set ts=2 sw=2 et:
  21. --- /dev/null
  22. +++ b/src/tangd-rotate-keys
  23. @@ -0,0 +1,85 @@
  24. +#!/bin/sh -e
  25. +# vim: set ts=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  26. +#
  27. +# Copyright (c) 2020 Red Hat, Inc.
  28. +# Author: Sergio Correia <scorreia@redhat.com>
  29. +#
  30. +# This program is free software: you can redistribute it and/or modify
  31. +# it under the terms of the GNU General Public License as published by
  32. +# the Free Software Foundation, either version 3 of the License, or
  33. +# (at your option) any later version.
  34. +#
  35. +# This program is distributed in the hope that it will be useful,
  36. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. +# GNU General Public License for more details.
  39. +#
  40. +# You should have received a copy of the GNU General Public License
  41. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  42. +#
  43. +
  44. +SUMMARY="Perform rotation of tang keys"
  45. +
  46. +usage() {
  47. + local _ret="${1:-1}"
  48. + exec >&2
  49. + echo "Usage: ${0} [-h] [-v] -d <KEYDIR>"
  50. + echo
  51. + echo "${SUMMARY}"
  52. + echo
  53. + echo " -d KEYDIR The directory with the keys, e.g. /var/db/tang"
  54. + echo
  55. + echo " -h Display this usage information"
  56. + echo
  57. + echo " -v Verbose. Display additional info on keys created/rotated"
  58. + echo
  59. + exit "${_ret}"
  60. +}
  61. +
  62. +log() {
  63. + local _msg="${1}"
  64. + local _verbose="${2:-}"
  65. + [ -z "${_verbose}" ] && return 0
  66. + echo "${_msg}" >&2
  67. +}
  68. +
  69. +error() {
  70. + log "${1}" 1
  71. + usage 1
  72. +}
  73. +
  74. +JWKDIR=
  75. +VERBOSE=
  76. +while getopts "hvd:" o; do
  77. + case "${o}" in
  78. + d) JWKDIR="${OPTARG}";;
  79. + h) usage 0;;
  80. + v) VERBOSE=1;;
  81. + *) usage 1;;
  82. + esac
  83. +done
  84. +
  85. +[ -z "${JWKDIR}" ] && error "Please specify the keys directory with -d switch"
  86. +[ -r "${JWKDIR}" ] || error "Error trying to access JWK directory '${JWKDIR}'"
  87. +
  88. +cd "${JWKDIR}" || error "Unable to change to keys directory '${JWKDIR}'"
  89. + # Disable advertisement of current keys.
  90. + for key in *.jwk; do
  91. + [ -r "${key}" ] || continue
  92. + mv -f -- "${key}" ."${key}"
  93. + log "Disabled advertisement of key ${key} -> .${key}" "${VERBOSE}"
  94. + done
  95. +
  96. + # Create a new set of keys.
  97. + DEFAULT_THP_HASH="S256"
  98. + for alg in "ES512" "ECMR"; do
  99. + json="$(printf '{"alg": "%s"}' "${alg}")"
  100. + jwe="$(jose jwk gen --input "${json}")"
  101. + thp="$(printf '%s' "${jwe}" | jose jwk thp --input=- \
  102. + -a "${DEFAULT_THP_HASH}")"
  103. + echo "${jwe}" > "${thp}.jwk"
  104. + log "Created new key ${thp}.jwk" "${VERBOSE}"
  105. + done
  106. +cd - >/dev/null
  107. +
  108. +log "Keys rotated successfully" "${VERBOSE}"