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