summaryrefslogtreecommitdiff
path: root/scripts/keygeneration/accessory/common_leverage_hammer.sh
blob: 7c51af1e0db61c641c626fa49a518f44911f3cd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash

# Copyright 2017 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Load common constants and functions.
. "$(dirname "$0")/../common.sh"

: "${HAS_ARG_KEYNAME:=}"

usage() {
  if [[ -n "${HAS_ARG_KEYNAME}" ]]; then
    cat <<EOF
Usage: ${PROG} <keyname> [options]

Arguments:
  keyname:                   Name of the hammer device (e.g. Staff, Wand).

Options:
  -o, --output_dir <dir>:    Where to write the keys (default is cwd)
EOF
  else
    cat <<EOF
Usage: ${PROG} [options]

Options:
  -o, --output_dir <dir>:    Where to write the keys (default is cwd)
EOF
  fi

  if [[ $# -ne 0 ]]; then
    die "$*"
  else
    exit 0
  fi
}

generate_rsa3072_exp3_key() {
  local output_dir="$1"
  local key_name="$2"

  # Generate RSA key.
  openssl genrsa -3 -out "${output_dir}/temp.pem" 3072

  # Create a keypair from an RSA .pem file generated above.
  futility create "${output_dir}/temp.pem" "${output_dir}/key_${key_name}"

  # Best attempt to securely delete the temp.pem file.
  shred --remove "${output_dir}/temp.pem"
}

# To generate a keypair with the same algorithm of Hammer and rename the kepair
# to specific accessory's name.
leverage_hammer_to_create_key() {
  local output_dir="${PWD}"
  local key_name=""

  while [[ $# -gt 0 ]]; do
    case "$1" in
    -h|--help)
      usage
      ;;
    -o|--output_dir)
      output_dir="$2"
      if [[ ! -d "${output_dir}" ]]; then
        die "output dir (\"${output_dir}\") doesn't exist."
      fi
      shift
      ;;
    -*)
      usage "Unknown option: \"$1\""
      ;;
    *)
      if [[ -n "${key_name}" ]]; then
        usage "Unknown argument \"$1\""
      fi
      key_name="$1"
      ;;
    esac
    shift
  done

  if [[ -z "${key_name}" ]]; then
    usage "Missing key name"
  fi

  generate_rsa3072_exp3_key "${output_dir}" "${key_name}"
}