You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

verify-commits.sh 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/sh
  2. # Copyright (c) 2014-2016 The Starwels developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. DIR=$(dirname "$0")
  6. [ "/${DIR#/}" != "$DIR" ] && DIR=$(dirname "$(pwd)/$0")
  7. echo "Using verify-commits data from ${DIR}"
  8. VERIFIED_ROOT=$(cat "${DIR}/trusted-git-root")
  9. VERIFIED_SHA512_ROOT=$(cat "${DIR}/trusted-sha512-root-commit")
  10. REVSIG_ALLOWED=$(cat "${DIR}/allow-revsig-commits")
  11. HAVE_FAILED=false
  12. HAVE_GNU_SHA512=1
  13. [ ! -x "$(which sha512sum)" ] && HAVE_GNU_SHA512=0
  14. if [ x"$1" = "x" ]; then
  15. CURRENT_COMMIT="HEAD"
  16. else
  17. CURRENT_COMMIT="$1"
  18. fi
  19. if [ "${CURRENT_COMMIT#* }" != "$CURRENT_COMMIT" ]; then
  20. echo "Commit must not contain spaces?" > /dev/stderr
  21. exit 1
  22. fi
  23. VERIFY_TREE=0
  24. if [ x"$2" = "x--tree-checks" ]; then
  25. VERIFY_TREE=1
  26. fi
  27. NO_SHA1=1
  28. PREV_COMMIT=""
  29. while true; do
  30. if [ "$CURRENT_COMMIT" = $VERIFIED_ROOT ]; then
  31. echo "There is a valid path from "$CURRENT_COMMIT" to $VERIFIED_ROOT where all commits are signed!"
  32. exit 0;
  33. fi
  34. if [ "$CURRENT_COMMIT" = $VERIFIED_SHA512_ROOT ]; then
  35. if [ "$VERIFY_TREE" = "1" ]; then
  36. echo "All Tree-SHA512s matched up to $VERIFIED_SHA512_ROOT" > /dev/stderr
  37. fi
  38. VERIFY_TREE=0
  39. NO_SHA1=0
  40. fi
  41. if [ "$NO_SHA1" = "1" ]; then
  42. export STARWELS_VERIFY_COMMITS_ALLOW_SHA1=0
  43. else
  44. export STARWELS_VERIFY_COMMITS_ALLOW_SHA1=1
  45. fi
  46. if [ "${REVSIG_ALLOWED#*$CURRENT_COMMIT}" != "$REVSIG_ALLOWED" ]; then
  47. export STARWELS_VERIFY_COMMITS_ALLOW_REVSIG=1
  48. else
  49. export STARWELS_VERIFY_COMMITS_ALLOW_REVSIG=0
  50. fi
  51. if ! git -c "gpg.program=${DIR}/gpg.sh" verify-commit "$CURRENT_COMMIT" > /dev/null; then
  52. if [ "$PREV_COMMIT" != "" ]; then
  53. echo "No parent of $PREV_COMMIT was signed with a trusted key!" > /dev/stderr
  54. echo "Parents are:" > /dev/stderr
  55. PARENTS=$(git show -s --format=format:%P $PREV_COMMIT)
  56. for PARENT in $PARENTS; do
  57. git show -s $PARENT > /dev/stderr
  58. done
  59. else
  60. echo "$CURRENT_COMMIT was not signed with a trusted key!" > /dev/stderr
  61. fi
  62. exit 1
  63. fi
  64. # We always verify the top of the tree
  65. if [ "$VERIFY_TREE" = 1 -o "$PREV_COMMIT" = "" ]; then
  66. IFS_CACHE="$IFS"
  67. IFS='
  68. '
  69. for LINE in $(git ls-tree --full-tree -r "$CURRENT_COMMIT"); do
  70. case "$LINE" in
  71. "12"*)
  72. echo "Repo contains symlinks" > /dev/stderr
  73. IFS="$IFS_CACHE"
  74. exit 1
  75. ;;
  76. esac
  77. done
  78. IFS="$IFS_CACHE"
  79. FILE_HASHES=""
  80. for FILE in $(git ls-tree --full-tree -r --name-only "$CURRENT_COMMIT" | LC_ALL=C sort); do
  81. if [ "$HAVE_GNU_SHA512" = 1 ]; then
  82. HASH=$(git cat-file blob "$CURRENT_COMMIT":"$FILE" | sha512sum | { read FIRST OTHER; echo $FIRST; } )
  83. else
  84. HASH=$(git cat-file blob "$CURRENT_COMMIT":"$FILE" | shasum -a 512 | { read FIRST OTHER; echo $FIRST; } )
  85. fi
  86. [ "$FILE_HASHES" != "" ] && FILE_HASHES="$FILE_HASHES"'
  87. '
  88. FILE_HASHES="$FILE_HASHES$HASH $FILE"
  89. done
  90. if [ "$HAVE_GNU_SHA512" = 1 ]; then
  91. TREE_HASH="$(echo "$FILE_HASHES" | sha512sum)"
  92. else
  93. TREE_HASH="$(echo "$FILE_HASHES" | shasum -a 512)"
  94. fi
  95. HASH_MATCHES=0
  96. MSG="$(git show -s --format=format:%B "$CURRENT_COMMIT" | tail -n1)"
  97. case "$MSG -" in
  98. "Tree-SHA512: $TREE_HASH")
  99. HASH_MATCHES=1;;
  100. esac
  101. if [ "$HASH_MATCHES" = "0" ]; then
  102. echo "Tree-SHA512 did not match for commit $CURRENT_COMMIT" > /dev/stderr
  103. exit 1
  104. fi
  105. fi
  106. PARENTS=$(git show -s --format=format:%P "$CURRENT_COMMIT")
  107. for PARENT in $PARENTS; do
  108. PREV_COMMIT="$CURRENT_COMMIT"
  109. CURRENT_COMMIT="$PARENT"
  110. break
  111. done
  112. done