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