c75ad6575a3f1d6703c1966b6eaa87a7a3d32448
[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="0"
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_host_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             *)
38                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
39                     failure "Unknown option '$1'.
40 Type '$PGRM help' for usage."
41                 fi
42                 hostName="$1"
43                 shift
44                 break
45                 ;;
46         esac
47 done
48
49 userID="ssh://${hostName}"
50
51 # set key parameters
52 keyParameters=\
53 "Key-Type: $keyType
54 Key-Length: $keyLength
55 Key-Usage: $keyUsage
56 Name-Real: $userID
57 Expire-Date: $keyExpire"
58
59 echo "The following key parameters will be used for the host key:"
60 echo "$keyParameters"
61
62 read -p "Generate key? (Y/n) " OK; OK=${OK:=Y}
63 if [ ${OK/y/Y} != 'Y' ] ; then
64         failure "aborting."
65 fi
66
67 # add commit command
68 # must include blank line!
69 keyParameters=\
70 "${keyParameters}
71
72 %commit
73 %echo done"
74
75 # create host home
76 mkdir -p "$GNUPGHOME_HOST"
77 chmod 700 "$GNUPGHOME_HOST"
78
79 log verbose "generating host key..."
80 echo "$keyParameters" | gpg_host --batch --gen-key
81
82 # find the key fingerprint of the newly converted key
83 HOST_FINGERPRINT=$(fingerprint_host_key)
84 export HOST_FINGERPRINT
85
86 # translate the private key to ssh format, and export to a file
87 # for sshs usage.
88 # NOTE: assumes that the primary key is the proper key to use
89 log debug "exporting new secret key to ssh format..."
90 (umask 077 && \
91         gpg_host --export-secret-key "$HOST_FINGERPRINT" | \
92         openpgp2ssh "$fingerprint" > "${MHDATADIR}/ssh_host_rsa_key")
93 log info "SSH host private key output to file: ${MHDATADIR}/ssh_host_rsa_key"
94
95 log debug "creating ssh public key..."
96 ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "${MHDATADIR}/ssh_host_rsa_key.pub"
97 log info "SSH host public key output to file: ${MHDATADIR}/ssh_host_rsa_key.pub"
98
99 # export public key to file
100 gpg_host_export_to_ssh_file
101
102 # show info about new key
103 show_key
104
105 }