2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
4 # Monkeysphere host gen-key subcommand
6 # The monkeysphere scripts are written by:
7 # Jameson Rollins <jrollins@finestructure.net>
8 # Jamie McClelland <jm@mayfirst.org>
9 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
11 # They are Copyright 2008-2009, and are all released under the GPL,
17 local keyLength="2048"
20 local hostName=$(hostname -f)
25 # check for presense of secret key
26 # FIXME: is this the proper test to be doing here?
27 fingerprint_host_key >/dev/null \
28 && failure "An OpenPGP host key already exists."
38 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
39 failure "Unknown option '$1'.
40 Type '$PGRM help' for usage."
49 userID="ssh://${hostName}"
54 Key-Length: $keyLength
57 Expire-Date: $keyExpire"
59 echo "The following key parameters will be used for the host key:"
62 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
63 if [ ${OK/y/Y} != 'Y' ] ; then
68 # must include blank line!
76 mkdir -p "$GNUPGHOME_HOST"
77 chmod 700 "$GNUPGHOME_HOST"
79 log verbose "generating host key..."
80 echo "$keyParameters" | gpg_host --batch --gen-key
82 # find the key fingerprint of the newly generated key
83 fingerprint=$(fingerprint_host_key)
85 # translate the private key to ssh format, and export to a file
87 # NOTE: assumes that the primary key is the proper key to use
88 log debug "exporting new secret key to ssh format..."
90 gpg_host --export-secret-key "$fingerprint" | \
91 openpgp2ssh "$fingerprint" > "${MHDATADIR}/ssh_host_rsa_key")
92 log info "SSH host private key output to file: ${MHDATADIR}/ssh_host_rsa_key"
94 log debug "creating ssh public key..."
95 ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "${MHDATADIR}/ssh_host_rsa_key.pub"
96 log info "SSH host public key output to file: ${MHDATADIR}/ssh_host_rsa_key.pub"
98 log debug "exporting openpgp public key..."
99 gpg_host --export-options export-minimal --armor --export "0x${fingerprint}!" > "${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
100 log info "SSH host public key in OpenPGP form: ${MHDATADIR}/ssh_host_rsa_key.pub.gpg"
102 # show info about new key