summaryrefslogtreecommitdiff
path: root/src/subcommands/mh/gen_key
blob: aad213a3cbe272f2a9bc120d7451d99d607de3c3 (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_server_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_server_key)
  73. # export host ownertrust to authentication keyring
  74. log verbose "setting ultimate owner trust for host key..."
  75. echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
  76. # translate the private key to ssh format, and export to a file
  77. # for sshs usage.
  78. # NOTE: assumes that the primary key is the proper key to use
  79. (umask 077 && \
  80. gpg_host --export-secret-key "$fingerprint" | \
  81. openpgp2ssh "$fingerprint" > "${SYSDATADIR}/ssh_host_rsa_key")
  82. log info "SSH host private key output to file: ${SYSDATADIR}/ssh_host_rsa_key"
  83. ssh-keygen -y -f "${SYSDATADIR}/ssh_host_rsa_key" > "${SYSDATADIR}/ssh_host_rsa_key.pub"
  84. log info "SSH host public key output to file: ${SYSDATADIR}/ssh_host_rsa_key.pub"
  85. gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  86. log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  87. # show info about new key
  88. show_key
  89. }