summaryrefslogtreecommitdiff
path: root/src/share/mh/gen_key
blob: eb951cf1f112c1e7efd12fce0852c316c8076002 (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="0"
  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. *)
  33. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  34. failure "Unknown option '$1'.
  35. Type '$PGRM help' for usage."
  36. fi
  37. hostName="$1"
  38. shift
  39. break
  40. ;;
  41. esac
  42. done
  43. userID="ssh://${hostName}"
  44. # set key parameters
  45. keyParameters=\
  46. "Key-Type: $keyType
  47. Key-Length: $keyLength
  48. Key-Usage: $keyUsage
  49. Name-Real: $userID
  50. Expire-Date: $keyExpire"
  51. echo "The following key parameters will be used for the host key:"
  52. echo "$keyParameters"
  53. read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
  54. if [ ${OK/y/Y} != 'Y' ] ; then
  55. failure "aborting."
  56. fi
  57. # add commit command
  58. # must include blank line!
  59. keyParameters=\
  60. "${keyParameters}
  61. %commit
  62. %echo done"
  63. # create host home
  64. mkdir -p "$GNUPGHOME_HOST"
  65. chmod 700 "$GNUPGHOME_HOST"
  66. log verbose "generating host key..."
  67. echo "$keyParameters" | gpg_host --batch --gen-key
  68. # find the key fingerprint of the newly generated key
  69. fingerprint=$(fingerprint_host_key)
  70. # translate the private key to ssh format, and export to a file
  71. # for sshs usage.
  72. # NOTE: assumes that the primary key is the proper key to use
  73. log debug "exporting new secret key to ssh format..."
  74. (umask 077 && \
  75. gpg_host --export-secret-key "$fingerprint" | \
  76. openpgp2ssh "$fingerprint" > "${MHDATADIR}/ssh_host_rsa_key")
  77. log info "SSH host private key output to file: ${MHDATADIR}/ssh_host_rsa_key"
  78. log debug "creating ssh public key..."
  79. ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "${MHDATADIR}/ssh_host_rsa_key.pub"
  80. log info "SSH host public key output to file: ${MHDATADIR}/ssh_host_rsa_key.pub"
  81. log debug "exporting openpgp public key..."
  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. }