tangd-update 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #!/bin/bash
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2016 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. TMP='{"protected":{"cty":"jwk-set+json"}}'
  21. trap 'exit' ERR
  22. shopt -s nullglob
  23. HASHES=`jose alg -k hash`
  24. if [ $# -ne 2 ] || [ ! -d "$1" ]; then
  25. echo "Usage: $0 <jwkdir> <cachedir>" >&2
  26. exit 1
  27. fi
  28. [ ! -d "$2" ] && mkdir -p -m 0700 "$2"
  29. src=`realpath "$1"`
  30. dst=`realpath "$2"`
  31. payl=()
  32. sign=()
  33. for jwk in $src/*.jwk; do
  34. if jose jwk use -i "$jwk" -r -u sign -u verify; then
  35. sign+=("-s" "$TMP" "-k" "$jwk")
  36. payl+=("-i" "$jwk")
  37. elif jose jwk use -i "$jwk" -r -u deriveKey; then
  38. payl+=("-i" "$jwk")
  39. else
  40. echo "Skipping invalid key: $jwk" >&2
  41. fi
  42. done
  43. if [ ${#sign[@]} -gt 0 ]; then
  44. jose jwk pub -s "${payl[@]}" \
  45. | jose jws sig -I- "${sign[@]}" -o "$dst/.default.jws"
  46. mv -f "$dst/.default.jws" "$dst/default.jws"
  47. new=default.jws
  48. fi
  49. shopt -s dotglob
  50. for jwk in $src/*.jwk; do
  51. for hsh in $HASHES; do
  52. thp=`jose jwk thp -i "$jwk" -a $hsh`
  53. if jose jwk use -i "$jwk" -r -u deriveKey; then
  54. ln -sf "$jwk" "$dst/.$thp.jwk"
  55. mv -f "$dst/.$thp.jwk" "$dst/$thp.jwk"
  56. new="$new\n$thp.jwk"
  57. elif jose jwk use -i "$jwk" -r -u sign; then
  58. keys=("${sign[@]}" -s "$TMP" -k "$jwk")
  59. jose jwk pub -s "${payl[@]}" \
  60. | jose jws sig -I- "${keys[@]}" -o "$dst/.$thp.jws"
  61. mv -f "$dst/.$thp.jws" "$dst/$thp.jws"
  62. new="$new\n$thp.jws"
  63. fi
  64. done
  65. done
  66. for f in "$dst"/*; do
  67. b=`basename "$f"`
  68. echo -e "$new" | grep -q "^$b\$" || rm -f "$f"
  69. done