summaryrefslogtreecommitdiff
path: root/src/subcommands/mh/gen-key
blob: 9f260873362031dd638f48cbe2259f311caaef9b (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. local keyType="RSA"
  12. local keyLength="2048"
  13. local keyUsage="auth"
  14. local keyExpire
  15. local hostName=$(hostname -f)
  16. local userID
  17. local keyParameters
  18. local fingerprint
  19. # check for presense of secret key
  20. # FIXME: is this the proper test to be doing here?
  21. fingerprint_server_key >/dev/null \
  22. && failure "An OpenPGP host key already exists."
  23. # get options
  24. while true ; do
  25. case "$1" in
  26. -h|--hostname)
  27. hostName="$2"
  28. shift 2
  29. ;;
  30. -l|--length)
  31. keyLength="$2"
  32. shift 2
  33. ;;
  34. -e|--expire)
  35. keyExpire="$2"
  36. shift 2
  37. ;;
  38. *)
  39. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  40. failure "Unknown option '$1'.
  41. Type '$PGRM help' for usage."
  42. fi
  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