Merge commit 'jrollins/master'
[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 hostName=$(hostname -f)
17 local keyType="RSA"
18 local keyLength="2048"
19 local keyUsage="auth"
20 local keyExpire="0"
21 local userID
22
23 # check for presense of a key
24 [ "$HOST_FINGERPRINT" ] && \
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             *)
35                 if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
36                     failure "Unknown option '$1'.
37 Type '$PGRM help' for usage."
38                 fi
39                 break
40                 ;;
41         esac
42 done
43
44 hostName="$1"
45 userID="ssh://${hostName}"
46
47 # create host home
48 mkdir -p "$GNUPGHOME_HOST"
49 chmod 700 "$GNUPGHOME_HOST"
50
51 log debug "generating host key..."
52 gpg_host --batch --gen-key <<EOF
53 Key-Type: $keyType
54 Key-Length: $keyLength
55 Key-Usage: $keyUsage
56 Name-Real: $userID
57 Expire-Date: $keyExpire
58
59 %commit
60 %echo done
61
62 EOF
63
64 # find the key fingerprint of the newly converted key
65 HOST_FINGERPRINT=$(get_host_fingerprint)
66 export HOST_FINGERPRINT
67
68 # translate the private key to ssh format, and export to a file
69 # for sshs usage.
70 # NOTE: assumes that the primary key is the proper key to use
71 log debug "exporting ssh secret key..."
72 (umask 077 && \
73         gpg_host --export-secret-key "$HOST_FINGERPRINT" | \
74         openpgp2ssh "$HOST_FINGERPRINT" > "${MHDATADIR}/ssh_host_rsa_key")
75 log info "SSH host private key output to file: ${MHDATADIR}/ssh_host_rsa_key"
76
77 log debug "creating ssh public key..."
78 ssh-keygen -y -f "${MHDATADIR}/ssh_host_rsa_key" > "$HOST_KEY_PUB"
79 log info "SSH host public key output to file: $HOST_KEY_PUB"
80
81 # export public key to file
82 gpg_host_export_to_ssh_file
83
84 # show info about new key
85 show_key
86
87 }