check-hash 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env bash
  2. set -e
  3. # Helper to check a file matches its known hash
  4. # Call it with:
  5. # $1: the path of the file containing all the the expected hashes
  6. # $2: the full path to the temporary file that was downloaded, and
  7. # that is to be checked
  8. # $3: the final basename of the file, to which it will be ultimately
  9. # saved as, to be able to match it to the corresponding hashes
  10. # in the .hash file
  11. #
  12. # Exit codes:
  13. # 0: the hash file exists and the file to check matches all its hashes,
  14. # or the hash file does not exist
  15. # 1: unknown command-line option
  16. # 2: the hash file exists and the file to check does not match at least
  17. # one of its hashes
  18. # 3: the hash file exists and there was no hash to check the file against
  19. # 4: the hash file exists and at least one hash type is unknown
  20. while getopts :q OPT; do
  21. case "${OPT}" in
  22. q) exec >/dev/null;;
  23. \?) exit 1;;
  24. esac
  25. done
  26. shift $((OPTIND-1))
  27. h_file="${1}"
  28. file="${2}"
  29. base="${3}"
  30. # Does the hash-file exist?
  31. if [ -z "${h_file}" -o ! -f "${h_file}" ]; then
  32. exit 0
  33. fi
  34. # Check one hash for a file
  35. # $1: known hash
  36. # $2: file (full path)
  37. check_one_hash() {
  38. _h="${1}"
  39. _known="${2}"
  40. _file="${3}"
  41. # Note: md5 is supported, but undocumented on purpose.
  42. # Note: sha3 is not supported, since there is currently no implementation
  43. # (the NIST has yet to publish the parameters).
  44. # Note: 'none' means there is explicitly no hash for that file.
  45. case "${_h}" in
  46. none)
  47. return 0
  48. ;;
  49. md5|sha1) ;;
  50. sha224|sha256|sha384|sha512) ;;
  51. *) # Unknown hash, exit with error
  52. printf "ERROR: unknown hash '%s' for '%s'\n" \
  53. "${_h}" "${base}" >&2
  54. exit 4
  55. ;;
  56. esac
  57. # Do the hashes match?
  58. _hash=$( ${_h}sum "${_file}" |cut -d ' ' -f 1 )
  59. if [ "${_hash}" = "${_known}" ]; then
  60. printf "%s: OK (%s: %s)\n" "${base}" "${_h}" "${_hash}"
  61. return 0
  62. fi
  63. printf "ERROR: %s has wrong %s hash:\n" "${base}" "${_h}" >&2
  64. printf "ERROR: expected: %s\n" "${_known}" >&2
  65. printf "ERROR: got : %s\n" "${_hash}" >&2
  66. printf "ERROR: Incomplete download, or man-in-the-middle (MITM) attack\n" >&2
  67. exit 2
  68. }
  69. # Do we know one or more hashes for that file?
  70. nb_checks=0
  71. while read t h f; do
  72. case "${t}" in
  73. ''|'#'*)
  74. # Skip comments and empty lines
  75. continue
  76. ;;
  77. *)
  78. if [ "${f}" = "${base}" ]; then
  79. check_one_hash "${t}" "${h}" "${file}"
  80. : $((nb_checks++))
  81. fi
  82. ;;
  83. esac
  84. done <"${h_file}"
  85. if [ ${nb_checks} -eq 0 ]; then
  86. if [ -n "${BR2_ENFORCE_CHECK_HASH}" ]; then
  87. printf "ERROR: No hash found for %s\n" "${base}" >&2
  88. exit 3
  89. else
  90. printf "WARNING: No hash found for %s\n" "${base}" >&2
  91. fi
  92. fi