create_new_android_keys.sh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. # Copyright 2016 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. set -e
  6. usage() {
  7. cat <<EOF
  8. Usage: $0 DIR
  9. Generate Android's 4 framework key pairs at DIR. For detail, please refer to
  10. "Certificates and private keys" and "Manually generating keys" in
  11. https://source.android.com/devices/tech/ota/sign_builds.html.
  12. EOF
  13. if [[ $# -ne 0 ]]; then
  14. echo "ERROR: $*" >&2
  15. exit 1
  16. else
  17. exit 0
  18. fi
  19. }
  20. # Use the same SUBJECT used in Nexus.
  21. SUBJECT='/C=US/ST=California/L=Mountain View/O=Google Inc./OU=Android/CN=Android'
  22. # Generate .pk8 and .x509.pem at the given directory.
  23. make_pair() {
  24. local dir=$1
  25. local name=$2
  26. # Generate RSA key.
  27. openssl genrsa -3 -out "${dir}/temp.pem" 2048
  28. # Create a certificate with the public part of the key.
  29. openssl req -new -x509 -key "${dir}/temp.pem" -out "${dir}/${name}.x509.pem" \
  30. -days 10000 -subj "${SUBJECT}"
  31. # Create a PKCS#8-formatted version of the private key.
  32. openssl pkcs8 -in "${dir}/temp.pem" -topk8 -outform DER \
  33. -out "${dir}/${name}.pk8" -nocrypt
  34. # Best attempt to securely delete the temp.pem file.
  35. shred --remove "${dir}/temp.pem"
  36. }
  37. main() {
  38. local dir
  39. while [[ $# -gt 0 ]]; do
  40. case $1 in
  41. -h|--help)
  42. usage
  43. ;;
  44. -*)
  45. usage "Unknown option: $1"
  46. ;;
  47. *)
  48. break
  49. ;;
  50. esac
  51. done
  52. if [[ $# -ne 1 ]]; then
  53. usage "Invalid argument."
  54. fi
  55. dir=$1
  56. make_pair "${dir}" platform
  57. make_pair "${dir}" shared
  58. make_pair "${dir}" media
  59. make_pair "${dir}" releasekey
  60. }
  61. main "$@"