3
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
5
# Copyright © 2008-2013 Teddy Hogeborn
6
# Copyright © 2008-2013 Björn Påhlsson
7
8
# This program is free software: you can redistribute it and/or modify
8
9
# it under the terms of the GNU General Public License as published by
17
18
# You should have received a copy of the GNU General Public License
18
19
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
# Contact the authors at <mandos@fukt.bsnet.se>.
21
# Contact the authors at <mandos@recompile.se>.
30
KEYNAME="`hostname --fqdn`"
26
KEYDIR="/etc/keys/mandos"
31
KEYNAME="`hostname --fqdn 2>/dev/null || hostname`"
32
KEYCOMMENT="Mandos client key"
35
36
KEYCOMMENT_ORIG="$KEYCOMMENT"
39
if [ ! -d "$KEYDIR" ]; then
40
KEYDIR="/etc/mandos/keys"
39
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
40
--longoptions version,help,password,dir:,type:,length:,subtype:,sublength:,name:,email:,comment:,expire:,force \
44
TEMP=`getopt --options vhpF:d:t:l:s:L:n:e:c:x:f \
45
--longoptions version,help,password,passfile:,dir:,type:,length:,subtype:,sublength:,name:,email:,comment:,expire:,force \
41
46
--name "$0" -- "$@"`
49
54
$basename [ OPTIONS ]
50
55
Encrypted password creation:
51
56
$basename { -p | --password } [ --name NAME ] [ --dir DIR]
57
$basename { -F | --passfile } FILE [ --name NAME ] [ --dir DIR]
53
59
Key creation options:
54
60
-v, --version Show program's version number and exit
55
61
-h, --help Show this help message and exit
56
62
-d DIR, --dir DIR Target directory for key files
57
-t TYPE, --type TYPE Key type. Default is DSA.
63
-t TYPE, --type TYPE Key type. Default is RSA.
58
64
-l BITS, --length BITS
59
Key length in bits. Default is 2048.
65
Key length in bits. Default is 4096.
60
66
-s TYPE, --subtype TYPE
61
Subkey type. Default is ELG-E.
67
Subkey type. Default is RSA.
62
68
-L BITS, --sublength BITS
63
Subkey length in bits. Default is 2048.
69
Subkey length in bits. Default is 4096.
64
70
-n NAME, --name NAME Name of key. Default is the FQDN.
65
71
-e ADDRESS, --email ADDRESS
66
72
Email address of key. Default is empty.
67
73
-c TEXT, --comment TEXT
68
Comment field for key. The default value is
74
Comment field for key. The default is empty.
70
75
-x TIME, --expire TIME
71
76
Key expire time. Default is no expiration.
72
77
See gpg(1) for syntax.
73
-f, --force Force overwriting old keys.
78
-f, --force Force overwriting old key files.
75
80
Password creation options:
76
-p, --password Create an encrypted password using the keys in
81
-p, --password Create an encrypted password using the key in
82
the key directory. All options other than
83
--dir and --name are ignored.
84
-F FILE, --passfile FILE
85
Encrypt a password from FILE using the key in
77
86
the key directory. All options other than
78
87
--dir and --name are ignored.
85
94
-p|--password) mode=password; shift;;
95
-F|--passfile) mode=password; PASSFILE="$2"; shift 2;;
86
96
-d|--dir) KEYDIR="$2"; shift 2;;
87
97
-t|--type) KEYTYPE="$2"; shift 2;;
88
98
-s|--subtype) SUBKEYTYPE="$2"; shift 2;;
148
158
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
151
if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
152
&& [ "$FORCE" -eq 0 ]; then
161
if [ \( -e "$SECKEYFILE" -o -e "$PUBKEYFILE" \) \
162
-a "$FORCE" -eq 0 ]; then
153
163
echo "Refusing to overwrite old key files; use --force" >&2
181
191
shred --remove \"$RINGDIR\"/sec*;
182
192
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
183
193
rm --recursive --force \"$RINGDIR\";
194
tty --quiet && stty echo; \
189
201
if [ "$mode" = keygen ]; then
190
202
# Create batch file for GnuPG
191
203
cat >"$BATCHFILE" <<-EOF
192
204
Key-Type: $KEYTYPE
193
205
Key-Length: $KEYLENGTH
194
#Key-Usage: encrypt,sign,auth
195
207
Subkey-Type: $SUBKEYTYPE
196
208
Subkey-Length: $SUBKEYLENGTH
197
#Subkey-Usage: encrypt,sign,auth
209
Subkey-Usage: encrypt
198
210
Name-Real: $KEYNAME
223
Note: Due to entropy requirements, key generation could take
224
anything from a few minutes to SEVERAL HOURS. Please be
225
patient and/or supply the system with more entropy if needed.
231
# Make sure trustdb.gpg exists;
232
# this is a workaround for Debian bug #737128
233
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
234
--homedir "$RINGDIR" \
235
--import-ownertrust < /dev/null
209
236
# Generate a new key in the key rings
210
237
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
211
238
--homedir "$RINGDIR" --trust-model always \
212
239
--gen-key "$BATCHFILE"
213
240
rm --force "$BATCHFILE"
215
247
# Backup any old key files
216
248
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
217
249
2>/dev/null; then
231
263
FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
234
# Export keys from key rings to key files
266
# Export key from key rings to key files
235
267
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
236
268
--homedir "$RINGDIR" --armor --export-options export-minimal \
237
269
--comment "$FILECOMMENT" --output "$SECKEYFILE" \
244
276
if [ "$mode" = password ]; then
245
# Import keys into temporary key rings
277
# Import key into temporary key rings
246
278
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
247
279
--homedir "$RINGDIR" --trust-model always --armor \
248
280
--import "$SECKEYFILE"
254
286
FINGERPRINT="`gpg --quiet --batch --no-tty --no-options \
255
287
--enable-dsa2 --homedir \"$RINGDIR\" --trust-model always \
256
288
--fingerprint --with-colons \
257
| sed -n -e '/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
290
--expression='/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
259
292
test -n "$FINGERPRINT"
261
294
FILECOMMENT="Encrypted password for a Mandos client"
264
echo -n "Enter passphrase: " >&2
266
| gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
267
--homedir "$RINGDIR" --trust-model always --armor --encrypt \
268
--recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
296
while [ ! -s "$SECFILE" ]; do
297
if [ -n "$PASSFILE" ]; then
300
tty --quiet && stty -echo
301
echo -n "Enter passphrase: " >&2
303
tty --quiet && echo >&2
304
echo -n "Repeat passphrase: " >&2
310
if [ "$first" != "$second" ]; then
311
echo "Passphrase mismatch" >&2
312
touch "$RINGDIR"/mismatch
316
fi | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
317
--homedir "$RINGDIR" --trust-model always --armor \
318
--encrypt --sign --recipient "$FINGERPRINT" --comment \
319
"$FILECOMMENT" > "$SECFILE"
320
if [ -e "$RINGDIR"/mismatch ]; then
321
rm --force "$RINGDIR"/mismatch
276
333
fingerprint = $FINGERPRINT
336
sed --quiet --expression='
280
337
/^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
282
339
# Remove 24-bit Radix-64 checksum