Gaurav Shah | 5746845 | 2011-03-02 14:50:46 -0800 | [diff] [blame] | 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 | # Common key generation functions. |
| 7 | |
| 8 | SCRIPT_DIR="$(dirname "$0")" |
| 9 | |
| 10 | # 0 = (RSA1024 SHA1) |
| 11 | # 1 = (RSA1024 SHA256) |
| 12 | # 2 = (RSA1024 SHA512) |
| 13 | # 3 = (RSA2048 SHA1) |
| 14 | # 4 = (RSA2048 SHA256) |
| 15 | # 5 = (RSA2048 SHA512) |
| 16 | # 6 = (RSA4096 SHA1) |
| 17 | # 7 = (RSA4096 SHA256) |
| 18 | # 8 = (RSA4096 SHA512) |
| 19 | # 9 = (RSA8192 SHA1) |
| 20 | # 10 = (RSA8192 SHA256) |
| 21 | # 11 = (RSA8192 SHA512) |
| 22 | function alg_to_keylen { |
| 23 | echo $(( 1 << (10 + ($1 / 3)) )) |
| 24 | } |
| 25 | |
Gaurav Shah | 41f444a | 2011-04-12 17:05:37 -0700 | [diff] [blame] | 26 | # Default alrogithms. |
| 27 | ROOT_KEY_ALGOID=11 |
| 28 | RECOVERY_KEY_ALGOID=11 |
| 29 | |
| 30 | FIRMWARE_DATAKEY_ALGOID=7 |
| 31 | DEV_FIRMWARE_DATAKEY_ALGOID=7 |
| 32 | |
| 33 | RECOVERY_KERNEL_ALGOID=11 |
| 34 | INSTALLER_KERNEL_ALGOID=11 |
| 35 | KERNEL_SUBKEY_ALGOID=7 |
| 36 | KERNEL_DATAKEY_ALGOID=4 |
| 37 | |
| 38 | # Keyblock modes determine which boot modes a signing key is valid for use |
| 39 | # in verification. |
| 40 | FIRMWARE_KEYBLOCK_MODE=7 |
| 41 | DEV_FIRMWARE_KEYBLOCK_MODE=6 # Only allow in dev mode. |
| 42 | RECOVERY_KERNEL_KEYBLOCK_MODE=11 |
| 43 | KERNEL_KEYBLOCK_MODE=7 # Only allow in non-recovery. |
| 44 | INSTALLER_KERNEL_KEYBLOCK_MODE=10 # Only allow in Dev + Recovery. |
| 45 | |
| 46 | |
Gaurav Shah | 5746845 | 2011-03-02 14:50:46 -0800 | [diff] [blame] | 47 | # Emit .vbpubk and .vbprivk using given basename and algorithm |
| 48 | # NOTE: This function also appears in ../../utility/dev_make_keypair. Making |
| 49 | # the two implementations the same would require some common.sh, which is more |
| 50 | # likely to cause problems than just keeping an eye out for any differences. If |
| 51 | # you feel the need to change this file, check the history of that other file |
| 52 | # to see what may need updating here too. |
| 53 | function make_pair { |
| 54 | local base=$1 |
| 55 | local alg=$2 |
Gaurav Shah | 41f444a | 2011-04-12 17:05:37 -0700 | [diff] [blame] | 56 | local key_version=${3:-1} |
Gaurav Shah | 5746845 | 2011-03-02 14:50:46 -0800 | [diff] [blame] | 57 | local len=$(alg_to_keylen $alg) |
| 58 | |
Gaurav Shah | 41f444a | 2011-04-12 17:05:37 -0700 | [diff] [blame] | 59 | echo "creating $base keypair (version = $key_version)..." |
Gaurav Shah | 5746845 | 2011-03-02 14:50:46 -0800 | [diff] [blame] | 60 | |
| 61 | # make the RSA keypair |
| 62 | openssl genrsa -F4 -out "${base}_${len}.pem" $len |
| 63 | # create a self-signed certificate |
| 64 | openssl req -batch -new -x509 -key "${base}_${len}.pem" \ |
| 65 | -out "${base}_${len}.crt" |
| 66 | # generate pre-processed RSA public key |
| 67 | dumpRSAPublicKey -cert "${base}_${len}.crt" > "${base}_${len}.keyb" |
| 68 | |
| 69 | # wrap the public key |
| 70 | vbutil_key \ |
| 71 | --pack "${base}.vbpubk" \ |
| 72 | --key "${base}_${len}.keyb" \ |
Gaurav Shah | 41f444a | 2011-04-12 17:05:37 -0700 | [diff] [blame] | 73 | --version "${key_version}" \ |
Gaurav Shah | 5746845 | 2011-03-02 14:50:46 -0800 | [diff] [blame] | 74 | --algorithm $alg |
| 75 | |
| 76 | # wrap the private key |
| 77 | vbutil_key \ |
| 78 | --pack "${base}.vbprivk" \ |
| 79 | --key "${base}_${len}.pem" \ |
| 80 | --algorithm $alg |
| 81 | |
| 82 | # remove intermediate files |
| 83 | rm -f "${base}_${len}.pem" "${base}_${len}.crt" "${base}_${len}.keyb" |
| 84 | } |
| 85 | |
| 86 | |
| 87 | # Emit a .keyblock containing flags and a public key, signed by a private key |
| 88 | # flags are the bitwise OR of these (passed in decimal, though) |
| 89 | # 0x01 Developer switch off |
| 90 | # 0x02 Developer switch on |
| 91 | # 0x04 Not recovery mode |
| 92 | # 0x08 Recovery mode |
| 93 | function make_keyblock { |
| 94 | local base=$1 |
| 95 | local flags=$2 |
| 96 | local pubkey=$3 |
| 97 | local signkey=$4 |
| 98 | |
| 99 | echo "creating $base keyblock..." |
| 100 | |
| 101 | # create it |
| 102 | vbutil_keyblock \ |
| 103 | --pack "${base}.keyblock" \ |
| 104 | --flags $flags \ |
| 105 | --datapubkey "${pubkey}.vbpubk" \ |
| 106 | --signprivate "${signkey}.vbprivk" |
| 107 | |
| 108 | # verify it |
| 109 | vbutil_keyblock \ |
| 110 | --unpack "${base}.keyblock" \ |
| 111 | --signpubkey "${signkey}.vbpubk" |
| 112 | } |
| 113 | |
| 114 | |