dev_make_keypair 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash -e
  2. # Copyright (c) 2010 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. #
  6. # Check args first.
  7. if [ "$#" -lt "1" ]; then
  8. cat <<EOF 1>&2
  9. Usage: ${0##*/} BASENAME [ALG]
  10. This creates BASENAME.vbpubk and BASENAME.vbprivk pairs for use in signing
  11. developer files. This also creates a BASENAME.keyblock file containing the
  12. BASENAME.vbpubk, which can be used to sign a developer kernel.
  13. If specified, ALG is one of:
  14. 0 = RSA1024 with SHA1
  15. 1 = RSA1024 with SHA256
  16. 2 = RSA1024 with SHA512
  17. 3 = RSA2048 with SHA1
  18. 4 = RSA2048 with SHA256
  19. 5 = RSA2048 with SHA512
  20. 6 = RSA4096 with SHA1
  21. 7 = RSA4096 with SHA256
  22. 8 = RSA4096 with SHA512
  23. 9 = RSA8192 with SHA1
  24. 10 = RSA8192 with SHA256
  25. 11 = RSA8192 with SHA512
  26. If ALG is not specified, a default value will be used.
  27. EOF
  28. exit 1
  29. fi
  30. # Compute the key length assuming the sizes shown above.
  31. function alg_to_keylen {
  32. echo $(( 1 << (10 + ($1 / 3)) ))
  33. }
  34. # Emit .vbpubk and .vbprivk using given basename and algorithm.
  35. function make_pair {
  36. local base=$1
  37. local alg=$2
  38. local len=$(alg_to_keylen $alg)
  39. # make the RSA keypair
  40. openssl genrsa -F4 -out "${base}_${len}.pem" $len
  41. # create a self-signed certificate
  42. openssl req -batch -new -x509 -key "${base}_${len}.pem" \
  43. -out "${base}_${len}.crt"
  44. # generate pre-processed RSA public key
  45. dumpRSAPublicKey -cert "${base}_${len}.crt" > "${base}_${len}.keyb"
  46. # wrap the public key
  47. futility vbutil_key \
  48. --pack "${base}.vbpubk" \
  49. --key "${base}_${len}.keyb" \
  50. --version 1 \
  51. --algorithm $alg
  52. # wrap the private key
  53. futility vbutil_key \
  54. --pack "${base}.vbprivk" \
  55. --key "${base}_${len}.pem" \
  56. --algorithm $alg
  57. # remove intermediate files
  58. rm -f "${base}_${len}.pem" "${base}_${len}.crt" "${base}_${len}.keyb"
  59. }
  60. # First create the .vbpubk and .vbprivk pair.
  61. make_pair "$1" "${2:-4}"
  62. # Now create a .keyblock to hold our .vbpubk. Since it's for developer use, it
  63. # won't be signed, just checksummed. Developer kernels can only be run in
  64. # non-recovery mode with the developer switch enabled, but it won't hurt us to
  65. # turn on all the flags bits anyway.
  66. futility vbutil_keyblock --pack "$1.keyblock" \
  67. --datapubkey "$1.vbpubk" --flags 15