keyset_version_check.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/bin/bash
  2. # Copyright 2014 The Chromium OS Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # Script that sanity checks a keyset to ensure actual key versions
  6. # match those set in key.versions.
  7. # Load common constants and variables.
  8. . "$(dirname "$0")/common.sh"
  9. # Abort on errors.
  10. set -e
  11. if [ $# -ne 1 ]; then
  12. cat <<EOF
  13. Usage: $0 <keyset directory>
  14. Sanity check a keyset directory for key versions.
  15. EOF
  16. exit 1
  17. fi
  18. KEY_DIR="$1"
  19. VERSION_FILE="${KEY_DIR}/key.versions"
  20. keyblock_version() {
  21. local keyblock="$1"
  22. echo "$(vbutil_keyblock --unpack "${keyblock}" | grep 'Data key version' |
  23. cut -f 2 -d : | tr -d ' ')"
  24. }
  25. key_version() {
  26. local key="$1"
  27. echo "$(vbutil_key --unpack "${key}" | grep 'Key Version' | cut -f 2 -d : |
  28. tr -d ' ')"
  29. }
  30. # Compare versions and print out error if there is a mismatch.
  31. check_versions() {
  32. local expected="$1"
  33. local got="$2"
  34. local expected_label="$3"
  35. local got_label="$4"
  36. if [[ ${expected} != ${got} ]]; then
  37. echo "ERROR: ${expected_label} version does not match ${got_label} version"
  38. echo "EXPECTED (${expected_label} version): ${expected}"
  39. echo "GOT (${got_label} version): ${got}"
  40. return 1
  41. fi
  42. return 0
  43. }
  44. main() {
  45. local testfail=0
  46. local expected_kkey="$(get_version kernel_key_version)"
  47. local expected_fkey="$(get_version firmware_key_version)"
  48. local expected_firmware="$(get_version firmware_version)"
  49. local expected_kernel="$(get_version kernel_version)"
  50. check_versions "${expected_firmware}" "${expected_kkey}" \
  51. "firmware" "kernel key" || testfail=1
  52. local got_fkey_keyblock="$(keyblock_version ${KEY_DIR}/firmware.keyblock)"
  53. local got_fkey="$(key_version ${KEY_DIR}/firmware_data_key.vbpubk)"
  54. local got_kkey_keyblock="$(keyblock_version ${KEY_DIR}/kernel.keyblock)"
  55. local got_ksubkey="$(key_version ${KEY_DIR}/kernel_subkey.vbpubk)"
  56. local got_kdatakey="$(key_version ${KEY_DIR}/kernel_data_key.vbpubk)"
  57. check_versions "${got_fkey_keyblock}" "${got_fkey}" "firmware keyblock key" \
  58. "firmware key" || testfail=1
  59. check_versions "${got_kkey_keyblock}" "${got_ksubkey}" "kernel keyblock key" \
  60. "kernel subkey" || testfail=1
  61. check_versions "${got_kdatakey}" "${got_ksubkey}" "kernel data key" \
  62. "kernel subkey" || testfail=1
  63. check_versions "${expected_fkey}" "${got_fkey}" "key.versions firmware key" \
  64. "firmware key" || testfail=1
  65. check_versions "${expected_kkey}" "${got_kdatakey}" "key.versions kernel key" \
  66. "kernel datakey" || testfail=1
  67. check_versions "${expected_kkey}" "${got_ksubkey}" "key.versions kernel key" \
  68. "kernel subkey" || testfail=1
  69. exit ${testfail}
  70. }
  71. main "$@"