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