summaryrefslogtreecommitdiff
path: root/scripts/keygeneration/accessory/create_new_gsc_key.sh
blob: a7e81c1a8205274da990232524d18d6f33399453 (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
#!/bin/bash

# Copyright 2022 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"

usage() {
  cat <<EOF
Usage: ${PROG} [options] <key_file_base_name>

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

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

generate_rsa3070_key() {
  local base_name="$1"
  local len="3070"

  echo "creating ${base_name} key pair..."

  # Make the RSA key pair.
  openssl genrsa -F4 -out "${base_name}.pem" "${len}"
  openssl rsa -in "${base_name}.pem" -outform PEM \
    -pubout -out "${base_name}.pem.pub"
}

main() {
  set -euo pipefail

  local base_name
  local output_dir="${PWD}"

  base_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 [[ -z ${base_name} ]]; then
        base_name="$1"
      else
        usage "Unknown argument $1"
      fi
      ;;
    esac
    shift
  done

  if [[ -z ${base_name} ]]; then
    usage "Key file base name missing"
  fi

  generate_rsa3070_key "${output_dir}/${base_name}"
}

main "$@"