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