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