add_loem_keys.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. # Copyright 2015 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. # Load common constants and functions.
  6. . "$(dirname "$0")/common.sh"
  7. usage() {
  8. cat <<EOF
  9. Usage: ${0##*/} <number of loem keys to add>
  10. If the existing keyset is not set up for loem usage, it will be converted.
  11. Note: Use 0 if you want to just convert an existing keyset.
  12. EOF
  13. exit ${1:-0}
  14. }
  15. convert_keyset_to_loem() {
  16. local f
  17. printf "Converting to loem keyset; continue? (y/N) "
  18. read f
  19. [[ ${f} == [yY] ]]
  20. for f in {firmware_data,root}_key.vb{pub,priv}k firmware.keyblock; do
  21. if [[ ${f} == "root_key.vbprivk" && ! -e ${f} ]]; then
  22. # For official keys, we won't have the private half of the root key.
  23. echo "Skipping ${f} for official keys"
  24. continue
  25. fi
  26. if [[ ${f} == *.vbprivk && ! -e ${f} ]]; then
  27. # For official keys, will be gpg wrapped.
  28. f+=".gpg"
  29. fi
  30. mv -i "${f}" "${f/./.loem1.}"
  31. done
  32. echo "[loem]" > loem.ini
  33. }
  34. main() {
  35. set -e -u
  36. if [[ $# -ne 1 || $1 == -* ]]; then
  37. usage
  38. fi
  39. # Keep `local` and assignment split so return values are checked.
  40. local firmware_key_version
  41. local num_keys highest_key k
  42. if [[ ! -e ${VERSION_FILE} ]]; then
  43. echo "missing ${VERSION_FILE} in ${PWD}; please create one" >&2
  44. exit 1
  45. fi
  46. firmware_key_version=$(get_version "firmware_key_version")
  47. # See if we need to convert the keyset first.
  48. if [[ -e root_key.vbpubk ]]; then
  49. convert_keyset_to_loem
  50. fi
  51. num_keys=$1
  52. highest_key=$(printf '%s\n' firmware.loem*.keyblock |
  53. sed -r 's:firmware.loem(.*).keyblock:\1:' |
  54. sort -n | tail -1)
  55. echo "There are ${highest_key} loem keys; ading ${num_keys} more"
  56. for ((k = highest_key + 1; k < highest_key + 1 + num_keys; ++k)); do
  57. echo "Generating LOEM ${k}"
  58. make_pair root_key.loem${k} ${ROOT_KEY_ALGOID}
  59. make_pair firmware_data_key.loem${k} ${FIRMWARE_DATAKEY_ALGOID} \
  60. ${firmware_key_version}
  61. make_keyblock firmware.loem${k} ${FIRMWARE_KEYBLOCK_MODE} \
  62. firmware_data_key.loem${k} root_key.loem${k}
  63. done
  64. echo
  65. echo "Don't forget to update loem.ini to allocate the keys!"
  66. }
  67. main "$@"