summaryrefslogtreecommitdiff
path: root/src/share/mh/gen_key
blob: a73d85e7e75bfa5b0929877b1d387354596d211a (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere host gen-key subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Jamie McClelland <jm@mayfirst.org>
  8. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. #
  10. # They are Copyright 2008-2009, and are all released under the GPL,
  11. # version 3 or later.
  12. gen_key() {
  13. local keyType="RSA"
  14. local keyLength="2048"
  15. local keyUsage="auth"
  16. local keyExpire
  17. local hostName=$(hostname -f)
  18. local userID
  19. local keyParameters
  20. local fingerprint
  21. # check for presense of secret key
  22. # FIXME: is this the proper test to be doing here?
  23. fingerprint_host_key >/dev/null \
  24. && failure "An OpenPGP host key already exists."
  25. # get options
  26. while true ; do
  27. case "$1" in
  28. -l|--length)
  29. keyLength="$2"
  30. shift 2
  31. ;;
  32. -e|--expire)
  33. keyExpire="$2"
  34. shift 2
  35. ;;
  36. *)
  37. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  38. failure "Unknown option '$1'.
  39. Type '$PGRM help' for usage."
  40. fi
  41. hostName="$1"
  42. shift;
  43. break
  44. ;;
  45. esac
  46. done
  47. userID="ssh://${hostName}"
  48. # prompt about key expiration if not specified
  49. keyExpire=$(get_gpg_expiration "$keyExpire")
  50. # set key parameters
  51. keyParameters=\
  52. "Key-Type: $keyType
  53. Key-Length: $keyLength
  54. Key-Usage: $keyUsage
  55. Name-Real: $userID
  56. Expire-Date: $keyExpire"
  57. echo "The following key parameters will be used for the host private key:"
  58. echo "$keyParameters"
  59. read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
  60. if [ ${OK/y/Y} != 'Y' ] ; then
  61. failure "aborting."
  62. fi
  63. # add commit command
  64. # must include blank line!
  65. keyParameters=\
  66. "${keyParameters}
  67. %commit
  68. %echo done"
  69. log verbose "generating host key..."
  70. echo "$keyParameters" | gpg_host --batch --gen-key
  71. # find the key fingerprint of the newly generated key
  72. fingerprint=$(fingerprint_host_key)
  73. # translate the private key to ssh format, and export to a file
  74. # for sshs usage.
  75. # NOTE: assumes that the primary key is the proper key to use
  76. (umask 077 && \
  77. gpg_host --export-secret-key "$fingerprint" | \
  78. openpgp2ssh "$fingerprint" > "${MHDATADIR}/ssh_host_rsa_key")
  79. log info "SSH host private key output to file: ${MHDATADIR}/ssh_host_rsa_key"
  80. ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "${MHDATADIR}/ssh_host_rsa_key.pub"
  81. log info "SSH host public key output to file: ${MHDATADIR}/ssh_host_rsa_key.pub"
  82. gpg_host "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
  83. log info "SSH host public key in OpenPGP form: ${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
  84. # show info about new key
  85. show_key
  86. }