• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright 2022 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
11usage() {
12  cat <<EOF
13Usage: ${PROG} [options] <key_file_base_name>
14
15Options:
16  -o, --output_dir <dir>:    Where to write the keys (default is cwd)
17EOF
18
19  if [[ $# -ne 0 ]]; then
20    die "$*"
21  else
22    exit 0
23  fi
24}
25
26generate_rsa3070_key() {
27  local base_name="$1"
28  local len="3070"
29
30  echo "creating ${base_name} key pair..."
31
32  # Make the RSA key pair.
33  openssl genrsa -F4 -out "${base_name}.pem" "${len}"
34  openssl rsa -in "${base_name}.pem" -outform PEM \
35    -pubout -out "${base_name}.pem.pub"
36}
37
38main() {
39  set -euo pipefail
40
41  local base_name
42  local output_dir="${PWD}"
43
44  base_name=""
45  while [[ $# -gt 0 ]]; do
46    case "$1" in
47    -h|--help)
48      usage
49      ;;
50    -o|--output_dir)
51      output_dir="$2"
52      if [[ ! -d "${output_dir}" ]]; then
53        die "output dir (${output_dir}) doesn't exist."
54      fi
55      shift
56      ;;
57    -*)
58      usage "Unknown option: $1"
59      ;;
60    *)
61      if [[ -z ${base_name} ]]; then
62        base_name="$1"
63      else
64        usage "Unknown argument $1"
65      fi
66      ;;
67    esac
68    shift
69  done
70
71  if [[ -z ${base_name} ]]; then
72    usage "Key file base name missing"
73  fi
74
75  generate_rsa3070_key "${output_dir}/${base_name}"
76}
77
78main "$@"
79