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