3
# Mandos key generator - create a new OpenPGP key for a Mandos client
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
# Contact the authors at <mandos@fukt.bsnet.se>.
28
KEYNAME="`hostname --fqdn`"
30
KEYCOMMENT="Mandos client key"
33
KEYCOMMENT_ORIG="$KEYCOMMENT"
36
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
37
--longoptions version,help,dir:,type:,length:,name:,email:,comment:,expire:,force \
42
Usage: `basename $0` [options]
45
-v, --version Show program's version number and exit
46
-h, --help Show this help message and exit
47
-d DIR, --dir DIR Target directory for key files
48
-t TYPE, --type TYPE Key type. Default is DSA.
49
-l BITS, --length BITS
50
Key length in bits. Default is 1024.
51
-n NAME, --name NAME Name of key. Default is the FQDN.
52
-e EMAIL, --email EMAIL
53
Email address of key. Default is empty.
54
-c COMMENT, --comment COMMENT
55
Comment field for key. The default value is
57
-x TIME, --expire TIME
58
Key expire time. Default is no expiration.
59
See gpg(1) for syntax.
60
-f, --force Force overwriting old keys.
67
-d|--dir) KEYDIR="$2"; shift 2;;
68
-t|--type) KEYTYPE="$2"; shift 2;;
69
-l|--length) KEYLENGTH="$2"; shift 2;;
70
-n|--name) KEYNAME="$2"; shift 2;;
71
-e|--email) KEYEMAIL="$2"; shift 2;;
72
-c|--comment) KEYCOMMENT="$2"; shift 2;;
73
-x|--expire) KEYEXPIRE="$2"; shift 2;;
74
-f|--force) FORCE=yes; shift;;
75
-v|--version) echo "$0 $VERSION"; exit;;
76
-h|--help) help; exit;;
78
*) echo "Internal error" >&2; exit 1;;
81
if [ "$#" -gt 0 ]; then
82
echo "Unknown arguments: '$@'" >&2
86
SECKEYFILE="$KEYDIR/seckey.txt"
87
PUBKEYFILE="$KEYDIR/pubkey.txt"
89
# Check for some invalid values
90
if [ -d "$KEYDIR" ]; then :; else
91
echo "$KEYDIR not a directory" >&2
94
if [ -w "$KEYDIR" ]; then :; else
95
echo "Directory $KEYDIR not writeable" >&2
99
if [ -z "$KEYTYPE" ]; then
100
echo "Empty key type" >&2
104
if [ -z "$KEYNAME" ]; then
105
echo "Empty key name" >&2
109
if [ -z "$KEYLENGTH" ] || [ "$KEYLENGTH" -lt 512 ]; then
110
echo "Invalid key length" >&2
114
if [ -z "$KEYEXPIRE" ]; then
115
echo "Empty key expiration" >&2
119
# Make FORCE be 0 or 1
121
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) FORCE=1;;
122
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
125
if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
126
&& [ "$FORCE" -eq 0 ]; then
127
echo "Refusing to overwrite old key files; use --force" >&2
131
# Set lines for GnuPG batch file
132
if [ -n "$KEYCOMMENT" ]; then
133
KEYCOMMENTLINE="Name-Comment: $KEYCOMMENT"
135
if [ -n "$KEYEMAIL" ]; then
136
KEYEMAILLINE="Name-Email: $KEYEMAIL"
140
BATCHFILE="`mktemp -t mandos-gpg-batch.XXXXXXXXXX`"
141
SECRING="`mktemp -t mandos-gpg-secring.XXXXXXXXXX`"
142
PUBRING="`mktemp -t mandos-gpg-pubring.XXXXXXXXXX`"
144
# Remove temporary files on exit
147
rm --force $PUBRING $BATCHFILE;
148
shred --remove $SECRING;
152
# Create batch file for GnuPG
153
cat >"$BATCHFILE" <<EOF
155
Key-Length: $KEYLENGTH
156
#Key-Usage: encrypt,sign,auth
160
Expire-Date: $KEYEXPIRE
168
# Generate a new key in the key rings
169
gpg --no-random-seed-file --quiet --batch --no-tty \
170
--no-default-keyring --no-options --batch \
171
--secret-keyring "$SECRING" --keyring "$PUBRING" \
172
--gen-key "$BATCHFILE"
173
rm --force "$BATCHFILE"
175
# Backup any old key files
176
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
178
shred --remove "$SECKEYFILE"
180
if cp --backup=numbered --force "$PUBKEYFILE" "$PUBKEYFILE" \
182
rm --force "$PUBKEYFILE"
185
FILECOMMENT="Mandos client key for $KEYNAME"
186
if [ "$KEYCOMMENT" != "$KEYCOMMENT_ORIG" ]; then
187
FILECOMMENT="$FILECOMMENT ($KEYCOMMENT)"
190
if [ -n "$KEYEMAIL" ]; then
191
FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
194
# Export keys from key rings to key files
195
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
196
--no-default-keyring --no-options --secret-keyring "$SECRING" \
197
--keyring "$PUBRING" --export-options export-minimal \
198
--comment "$FILECOMMENT" --output "$SECKEYFILE" \
200
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
201
--no-default-keyring --no-options --secret-keyring "$SECRING" \
202
--keyring "$PUBRING" --export-options export-minimal \
203
--comment "$FILECOMMENT" --output "$PUBKEYFILE" \
208
# Remove the key rings
209
shred --remove "$SECRING"
210
rm --force "$PUBRING"