summaryrefslogtreecommitdiff
path: root/src/share/mh/gen_key
blob: 3b9a2697a60b3fb1786e12baa94b796ffd39d86b (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere host gen-key subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Jamie McClelland <jm@mayfirst.org>
  8. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. #
  10. # They are Copyright 2008-2009, and are all released under the GPL,
  11. # version 3 or later.
  12. gen_key() {
  13. local hostName
  14. local keyType="RSA"
  15. local keyLength="2048"
  16. local keyUsage="auth"
  17. local keyExpire="0"
  18. local userID
  19. # get options
  20. while true ; do
  21. case "$1" in
  22. -l|--length)
  23. keyLength="$2"
  24. shift 2
  25. ;;
  26. *)
  27. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  28. failure "Unknown option '$1'.
  29. Type '$PGRM help' for usage."
  30. fi
  31. break
  32. ;;
  33. esac
  34. done
  35. hostName=${1:-$(hostname -f)}
  36. userID="ssh://${hostName}"
  37. # create host home
  38. mkdir -p "$GNUPGHOME_HOST"
  39. chmod 700 "$GNUPGHOME_HOST"
  40. log debug "generating host key..."
  41. gpg_host --batch --gen-key <<EOF
  42. Key-Type: $keyType
  43. Key-Length: $keyLength
  44. Key-Usage: $keyUsage
  45. Name-Real: $userID
  46. Expire-Date: $keyExpire
  47. %commit
  48. %echo done
  49. EOF
  50. # load the new host fpr into the fpr variable
  51. load_fingerprint_secret
  52. # export the host secret key to the monkeysphere ssh sec key file
  53. # NOTE: assumes that the primary key is the proper key to use
  54. log debug "creating ssh secret key file..."
  55. (umask 077 && \
  56. gpg_host --export-secret-key "$HOST_FINGERPRINT" | \
  57. openpgp2ssh "$HOST_FINGERPRINT" > "${MHDATADIR}/ssh_host_rsa_key")
  58. log info "SSH host secret key file: ${MHDATADIR}/ssh_host_rsa_key"
  59. # export the host public key to the monkeysphere ssh pub key file
  60. log debug "creating ssh public key file..."
  61. ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "$HOST_KEY_PUB"
  62. log info "SSH host public key file: $HOST_KEY_PUB"
  63. # export to gpg public key to file
  64. create_gpg_pub_file
  65. # show info about new key
  66. show_key
  67. }