| 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2017 The ChromiumOS Authors |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # Load common constants and functions. |
| 8 | # shellcheck source=../common.sh |
| 9 | . "$(dirname "$0")/../common.sh" |
| 10 | |
| 11 | : "${HAS_ARG_KEYNAME:=}" |
| 12 | |
| 13 | usage() { |
| 14 | if [[ -n "${HAS_ARG_KEYNAME}" ]]; then |
| 15 | cat <<EOF |
| 16 | Usage: ${PROG} <keyname> [options] |
| 17 | |
| 18 | Arguments: |
| 19 | keyname: Name of the hammer device (e.g. Staff, Wand). |
| 20 | |
| 21 | Options: |
| 22 | -o, --output_dir <dir>: Where to write the keys (default is cwd) |
| 23 | EOF |
| 24 | else |
| 25 | cat <<EOF |
| 26 | Usage: ${PROG} [options] |
| 27 | |
| 28 | Options: |
| 29 | -o, --output_dir <dir>: Where to write the keys (default is cwd) |
| 30 | EOF |
| 31 | fi |
| 32 | |
| 33 | if [[ $# -ne 0 ]]; then |
| 34 | die "$*" |
| 35 | else |
| 36 | exit 0 |
| 37 | fi |
| 38 | } |
| 39 | |
| 40 | generate_rsa3072_exp3_key() { |
| 41 | local output_dir="$1" |
| 42 | local key_name="$2" |
| 43 | |
| 44 | # Generate RSA key. |
| 45 | openssl genrsa -3 -out "${output_dir}/temp.pem" 3072 |
| 46 | |
| 47 | # Create a keypair from an RSA .pem file generated above. |
| 48 | futility create "${output_dir}/temp.pem" "${output_dir}/key_${key_name}" |
| 49 | |
| 50 | # Best attempt to securely delete the temp.pem file. |
| 51 | shred --remove "${output_dir}/temp.pem" |
| 52 | } |
| 53 | |
| 54 | # To generate a keypair with the same algorithm of Hammer and rename the kepair |
| 55 | # to specific accessory's name. |
| 56 | leverage_hammer_to_create_key() { |
| 57 | local output_dir="${PWD}" |
| 58 | local key_name="" |
| 59 | |
| 60 | while [[ $# -gt 0 ]]; do |
| 61 | case "$1" in |
| 62 | -h|--help) |
| 63 | usage |
| 64 | ;; |
| 65 | -o|--output_dir) |
| 66 | output_dir="$2" |
| 67 | if [[ ! -d "${output_dir}" ]]; then |
| 68 | die "output dir (\"${output_dir}\") doesn't exist." |
| 69 | fi |
| 70 | shift |
| 71 | ;; |
| 72 | -*) |
| 73 | usage "Unknown option: \"$1\"" |
| 74 | ;; |
| 75 | *) |
| 76 | if [[ -n "${key_name}" ]]; then |
| 77 | usage "Unknown argument \"$1\"" |
| 78 | fi |
| 79 | key_name="$1" |
| 80 | ;; |
| 81 | esac |
| 82 | shift |
| 83 | done |
| 84 | |
| 85 | if [[ -z "${key_name}" ]]; then |
| 86 | usage "Missing key name" |
| 87 | fi |
| 88 | |
| 89 | generate_rsa3072_exp3_key "${output_dir}" "${key_name}" |
| 90 | } |