create_new_keys.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/bin/bash
  2. # Copyright (c) 2011 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. #
  6. # Generate .vbpubk and .vbprivk pairs for use by developer builds. These should
  7. # be exactly like the real keys except that the private keys aren't secret.
  8. # Load common constants and functions.
  9. . "$(dirname "$0")/common.sh"
  10. usage() {
  11. cat <<EOF
  12. Usage: $0 [options]
  13. Options:
  14. --devkeyblock Also generate developer firmware keyblock and data key
  15. --android Also generate android keys
  16. --4k Use 4k keys instead of 8k (enables options below)
  17. --4k-root Use 4k key size for the root key
  18. --4k-recovery Use 4k key size for the recovery key
  19. --4k-recovery-kernel Use 4k key size for the recovery kernel data
  20. --4k-installer-kernel Use 4k key size for the installer kernel data
  21. --key-name <name> Name of the keyset (for key.versions)
  22. --output <dir> Where to write the keys (default is cwd)
  23. EOF
  24. if [[ $# -ne 0 ]]; then
  25. echo "ERROR: unknown option $*" >&2
  26. exit 1
  27. else
  28. exit 0
  29. fi
  30. }
  31. main() {
  32. set -e
  33. # Flag to indicate whether we should be generating a developer keyblock flag.
  34. local dev_keyblock="false"
  35. local android_keys="false"
  36. local root_key_algoid=${ROOT_KEY_ALGOID}
  37. local recovery_key_algoid=${RECOVERY_KEY_ALGOID}
  38. local recovery_kernel_algoid=${RECOVERY_KERNEL_ALGOID}
  39. local installer_kernel_algoid=${INSTALLER_KERNEL_ALGOID}
  40. local keyname
  41. local output_dir="${PWD}" setperms="false"
  42. while [[ $# -gt 0 ]]; do
  43. case $1 in
  44. --devkeyblock)
  45. echo "Will also generate developer firmware keyblock and data key."
  46. dev_keyblock="true"
  47. ;;
  48. --android)
  49. echo "Will also generate Android keys."
  50. android_keys="true"
  51. ;;
  52. --4k)
  53. root_key_algoid=${RSA4096_SHA512_ALGOID}
  54. recovery_key_algoid=${RSA4096_SHA512_ALGOID}
  55. recovery_kernel_algoid=${RSA4096_SHA512_ALGOID}
  56. installer_kernel_algoid=${RSA4096_SHA512_ALGOID}
  57. ;;
  58. --4k-root)
  59. root_key_algoid=${RSA4096_SHA512_ALGOID}
  60. ;;
  61. --4k-recovery)
  62. recovery_key_algoid=${RSA4096_SHA512_ALGOID}
  63. ;;
  64. --4k-recovery-kernel)
  65. recovery_kernel_algoid=${RSA4096_SHA512_ALGOID}
  66. ;;
  67. --4k-installer-kernel)
  68. installer_kernel_algoid=${RSA4096_SHA512_ALGOID}
  69. ;;
  70. --key-name)
  71. keyname="$2"
  72. shift
  73. ;;
  74. --output)
  75. output_dir="$2"
  76. setperms="true"
  77. if [[ -d "${output_dir}" ]]; then
  78. echo "ERROR: output dir (${output_dir}) already exists" >&2
  79. exit 1
  80. fi
  81. shift
  82. ;;
  83. -h|--help)
  84. usage
  85. ;;
  86. *)
  87. usage "$1"
  88. ;;
  89. esac
  90. shift
  91. done
  92. mkdir -p "${output_dir}"
  93. cd "${output_dir}"
  94. if [[ "${setperms}" == "true" ]]; then
  95. chmod 700 .
  96. fi
  97. if [[ ! -e "${VERSION_FILE}" ]]; then
  98. echo "No version file found. Creating default ${VERSION_FILE}."
  99. (
  100. if [[ -n "${keyname}" ]]; then
  101. echo "name=${keyname}"
  102. fi
  103. printf '%s_version=1\n' {firmware,kernel}{_key,}
  104. ) > "${VERSION_FILE}"
  105. fi
  106. local eckey_version fkey_version ksubkey_version kdatakey_version
  107. # Get the key versions for normal keypairs
  108. eckey_version=$(get_version "ec_key_version")
  109. fkey_version=$(get_version "firmware_key_version")
  110. # Firmware version is the kernel subkey version.
  111. ksubkey_version=$(get_version "firmware_version")
  112. # Kernel data key version is the kernel key version.
  113. kdatakey_version=$(get_version "kernel_key_version")
  114. # Create the normal keypairs
  115. make_pair ec_root_key ${EC_ROOT_KEY_ALGOID}
  116. make_pair ec_data_key ${EC_DATAKEY_ALGOID} ${eckey_version}
  117. make_pair root_key ${root_key_algoid}
  118. make_pair firmware_data_key ${FIRMWARE_DATAKEY_ALGOID} ${fkey_version}
  119. if [[ "${dev_keyblock}" == "true" ]]; then
  120. make_pair dev_firmware_data_key ${DEV_FIRMWARE_DATAKEY_ALGOID} ${fkey_version}
  121. fi
  122. make_pair kernel_subkey ${KERNEL_SUBKEY_ALGOID} ${ksubkey_version}
  123. make_pair kernel_data_key ${KERNEL_DATAKEY_ALGOID} ${kdatakey_version}
  124. # Create the recovery and factory installer keypairs
  125. make_pair recovery_key ${recovery_key_algoid}
  126. make_pair recovery_kernel_data_key ${recovery_kernel_algoid}
  127. make_pair installer_kernel_data_key ${installer_kernel_algoid}
  128. # Create the firmware keyblock for use only in Normal mode. This is redundant,
  129. # since it's never even checked during Recovery mode.
  130. make_keyblock firmware ${FIRMWARE_KEYBLOCK_MODE} firmware_data_key root_key
  131. # Ditto EC keyblock
  132. make_keyblock ec ${EC_KEYBLOCK_MODE} ec_data_key ec_root_key
  133. if [[ "${dev_keyblock}" == "true" ]]; then
  134. # Create the dev firmware keyblock for use only in Developer mode.
  135. make_keyblock dev_firmware ${DEV_FIRMWARE_KEYBLOCK_MODE} dev_firmware_data_key root_key
  136. fi
  137. # Create the recovery kernel keyblock for use only in Recovery mode.
  138. make_keyblock recovery_kernel ${RECOVERY_KERNEL_KEYBLOCK_MODE} recovery_kernel_data_key recovery_key
  139. # Create the normal kernel keyblock for use only in Normal mode.
  140. make_keyblock kernel ${KERNEL_KEYBLOCK_MODE} kernel_data_key kernel_subkey
  141. # Create the installer keyblock for use in Developer + Recovery mode
  142. # For use in Factory Install and Developer Mode install shims.
  143. make_keyblock installer_kernel ${INSTALLER_KERNEL_KEYBLOCK_MODE} installer_kernel_data_key recovery_key
  144. if [[ "${android_keys}" == "true" ]]; then
  145. mkdir android
  146. "${SCRIPT_DIR}"/create_new_android_keys.sh android
  147. fi
  148. if [[ "${setperms}" == "true" ]]; then
  149. find -type f -exec chmod 400 {} +
  150. find -type d -exec chmod 500 {} +
  151. fi
  152. # CAUTION: The public parts of most of these blobs must be compiled into the
  153. # firmware, which is built separately (and some of which can't be changed after
  154. # manufacturing). If you update these keys, you must coordinate the changes
  155. # with the BIOS people or you'll be unable to boot the resulting images.
  156. }
  157. main "$@"