aad213a3cbe272f2a9bc120d7451d99d607de3c3
[monkeysphere.git] / src / share / mh / gen_key
1 # -*-shell-script-*-
2 # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
3
4 # Monkeysphere host gen-key subcommand
5 #
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>
10 #
11 # They are Copyright 2008-2009, and are all released under the GPL,
12 # version 3 or later.
13
14 gen_key() {
15
16 local keyType="RSA"
17 local keyLength="2048"
18 local keyUsage="auth"
19 local keyExpire
20 local hostName=$(hostname -f)
21 local userID
22 local keyParameters
23 local fingerprint
24
25 # check for presense of secret key
26 # FIXME: is this the proper test to be doing here?
27 fingerprint_server_key >/dev/null \
28         && failure "An OpenPGP host key already exists."
29
30 # get options
31 while true ; do
32         case "$1" in
33             -l|--length)
34                 keyLength="$2"
35                 shift 2
36                 ;;
37             -e|--expire)
38                 keyExpire="$2"
39                 shift 2
40                 ;;
41             *)
42                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
43                     failure "Unknown option '$1'.
44 Type '$PGRM help' for usage."
45                 fi
46                 hostName="$1"
47                 shift;
48                 break
49                 ;;
50         esac
51 done
52
53 userID="ssh://${hostName}"
54
55 # prompt about key expiration if not specified
56 keyExpire=$(get_gpg_expiration "$keyExpire")
57
58 # set key parameters
59 keyParameters=\
60 "Key-Type: $keyType
61 Key-Length: $keyLength
62 Key-Usage: $keyUsage
63 Name-Real: $userID
64 Expire-Date: $keyExpire"
65
66 echo "The following key parameters will be used for the host private key:"
67 echo "$keyParameters"
68
69 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
70 if [ ${OK/y/Y} != 'Y' ] ; then
71         failure "aborting."
72 fi
73
74 # add commit command
75 # must include blank line!
76 keyParameters=\
77 "${keyParameters}
78
79 %commit
80 %echo done"
81
82 log verbose "generating host key..."
83 echo "$keyParameters" | gpg_host --batch --gen-key
84
85 # find the key fingerprint of the newly generated key
86 fingerprint=$(fingerprint_server_key)
87
88 # export host ownertrust to authentication keyring
89 log verbose "setting ultimate owner trust for host key..."
90 echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
91
92 # translate the private key to ssh format, and export to a file
93 # for sshs usage.
94 # NOTE: assumes that the primary key is the proper key to use
95 (umask 077 && \
96         gpg_host --export-secret-key "$fingerprint" | \
97         openpgp2ssh "$fingerprint" > "${SYSDATADIR}/ssh_host_rsa_key")
98 log info "SSH host private key output to file: ${SYSDATADIR}/ssh_host_rsa_key"
99 ssh-keygen -y -f "${SYSDATADIR}/ssh_host_rsa_key" > "${SYSDATADIR}/ssh_host_rsa_key.pub"
100 log info "SSH host public key output to file: ${SYSDATADIR}/ssh_host_rsa_key.pub"
101 gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
102 log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
103
104 # show info about new key
105 show_key
106
107 }