| 1 | #!/bin/bash |
| 2 | # Copyright 2018 The ChromiumOS Authors |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | # Load common constants and functions. |
| 7 | # shellcheck source=uefi_common.sh |
| 8 | . "$(dirname "$0")/uefi_common.sh" |
| 9 | |
| 10 | usage() { |
| 11 | cat <<EOF |
| 12 | Usage: ${PROG} [options] |
| 13 | |
| 14 | Generate key pairs for UEFI secure boot. |
| 15 | |
| 16 | Options: |
| 17 | --output <dir> Where to write the keys (default is cwd). |
| 18 | The base name must be '.../uefi'. |
| 19 | --no-pk Do not generate PK. |
| 20 | EOF |
| 21 | |
| 22 | if [[ $# -ne 0 ]]; then |
| 23 | die "unknown option $*" |
| 24 | else |
| 25 | exit 0 |
| 26 | fi |
| 27 | } |
| 28 | |
| 29 | main() { |
| 30 | set -e |
| 31 | |
| 32 | local generate_pk="true" |
| 33 | local output_dir="${PWD}" |
| 34 | |
| 35 | while [[ $# -gt 0 ]]; do |
| 36 | case $1 in |
| 37 | --output) |
| 38 | output_dir="$2" |
| 39 | shift |
| 40 | ;; |
| 41 | --no-pk) |
| 42 | info "Will not generate PK." |
| 43 | generate_pk="false" |
| 44 | ;; |
| 45 | -h|--help) |
| 46 | usage |
| 47 | ;; |
| 48 | *) |
| 49 | usage "Unknown option: $1" |
| 50 | ;; |
| 51 | esac |
| 52 | shift |
| 53 | done |
| 54 | |
| 55 | check_uefi_key_dir_name "${output_dir}" |
| 56 | pushd "${output_dir}" >/dev/null || die "Wrong output directory name" |
| 57 | |
| 58 | if [[ ! -e "${UEFI_VERSION_FILE}" ]]; then |
| 59 | echo "No version file found. Creating default ${UEFI_VERSION_FILE}." |
| 60 | printf '%s_key_version=1\n' {pk,kek,db,db_child} > "${UEFI_VERSION_FILE}" |
| 61 | fi |
| 62 | |
| 63 | local pk_key_version kek_key_version db_key_version db_child_key_version |
| 64 | |
| 65 | # Get the key versions for normal keypairs |
| 66 | pk_key_version=$(get_uefi_version "pk_key_version") |
| 67 | kek_key_version=$(get_uefi_version "kek_key_version") |
| 68 | db_key_version=$(get_uefi_version "db_key_version") |
| 69 | db_child_key_version=$(get_uefi_version "db_child_key_version") |
| 70 | |
| 71 | if [[ "${generate_pk}" == "true" ]]; then |
| 72 | make_pk_keypair "${pk_key_version}" |
| 73 | fi |
| 74 | make_kek_keypair "${kek_key_version}" |
| 75 | make_db_keypair "${db_key_version}" |
| 76 | make_db_child_keypair "${db_key_version}" "${db_child_key_version}" |
| 77 | |
| 78 | popd >/dev/null |
| 79 | } |
| 80 | |
| 81 | main "$@" |