summaryrefslogtreecommitdiff
path: root/src/subcommands/mh/gen-key
blob: 72b913823761d1d099da1df7c5a9817d6a999fe6 (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. -l|--length)
  28. keyLength="$2"
  29. shift 2
  30. ;;
  31. -e|--expire)
  32. keyExpire="$2"
  33. shift 2
  34. ;;
  35. *)
  36. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  37. failure "Unknown option '$1'.
  38. Type '$PGRM help' for usage."
  39. fi
  40. hostName="$1"
  41. shift;
  42. break
  43. ;;
  44. esac
  45. done
  46. userID="ssh://${hostName}"
  47. # prompt about key expiration if not specified
  48. keyExpire=$(get_gpg_expiration "$keyExpire")
  49. # set key parameters
  50. keyParameters=\
  51. "Key-Type: $keyType
  52. Key-Length: $keyLength
  53. Key-Usage: $keyUsage
  54. Name-Real: $userID
  55. Expire-Date: $keyExpire"
  56. echo "The following key parameters will be used for the host private key:"
  57. echo "$keyParameters"
  58. read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
  59. if [ ${OK/y/Y} != 'Y' ] ; then
  60. failure "aborting."
  61. fi
  62. # add commit command
  63. # must include blank line!
  64. keyParameters=\
  65. "${keyParameters}
  66. %commit
  67. %echo done"
  68. log verbose "generating host key..."
  69. echo "$keyParameters" | gpg_host --batch --gen-key
  70. # find the key fingerprint of the newly generated key
  71. fingerprint=$(fingerprint_server_key)
  72. # export host ownertrust to authentication keyring
  73. log verbose "setting ultimate owner trust for host key..."
  74. echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
  75. # translate the private key to ssh format, and export to a file
  76. # for sshs usage.
  77. # NOTE: assumes that the primary key is the proper key to use
  78. (umask 077 && \
  79. gpg_host --export-secret-key "$fingerprint" | \
  80. openpgp2ssh "$fingerprint" > "${SYSDATADIR}/ssh_host_rsa_key")
  81. log info "SSH host private key output to file: ${SYSDATADIR}/ssh_host_rsa_key"
  82. ssh-keygen -y -f "${SYSDATADIR}/ssh_host_rsa_key" > "${SYSDATADIR}/ssh_host_rsa_key.pub"
  83. log info "SSH host public key output to file: ${SYSDATADIR}/ssh_host_rsa_key.pub"
  84. gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  85. log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  86. # show info about new key
  87. show_key
  88. }