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