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>.
25
26
KEYDIR="/etc/keys/mandos"
30
KEYNAME="`hostname --fqdn`"
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;;
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
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.
209
231
# Generate a new key in the key rings
210
232
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
211
233
--homedir "$RINGDIR" --trust-model always \
212
234
--gen-key "$BATCHFILE"
213
235
rm --force "$BATCHFILE"
215
242
# Backup any old key files
216
243
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
217
244
2>/dev/null; then
231
258
FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
234
# Export keys from key rings to key files
261
# Export key from key rings to key files
235
262
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
236
263
--homedir "$RINGDIR" --armor --export-options export-minimal \
237
264
--comment "$FILECOMMENT" --output "$SECKEYFILE" \
244
271
if [ "$mode" = password ]; then
245
# Import keys into temporary key rings
272
# Import key into temporary key rings
246
273
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
247
274
--homedir "$RINGDIR" --trust-model always --armor \
248
275
--import "$SECKEYFILE"
262
289
FILECOMMENT="Encrypted password for a Mandos client"
265
echo -n "Enter passphrase: " >&2
266
head --lines=1 | tr --delete '\n' \
267
| gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
268
--homedir "$RINGDIR" --trust-model always --armor --encrypt \
269
--recipient "$FINGERPRINT" --comment "$FILECOMMENT" \
291
while [ ! -s "$SECFILE" ]; do
292
if [ -n "$PASSFILE" ]; then
295
tty --quiet && stty -echo
296
echo -n "Enter passphrase: " >&2
298
tty --quiet && echo >&2
299
echo -n "Repeat passphrase: " >&2
305
if [ "$first" != "$second" ]; then
306
echo "Passphrase mismatch" >&2
307
touch "$RINGDIR"/mismatch
311
fi | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
312
--homedir "$RINGDIR" --trust-model always --armor \
313
--encrypt --sign --recipient "$FINGERPRINT" --comment \
314
"$FILECOMMENT" > "$SECFILE"
315
if [ -e "$RINGDIR"/mismatch ]; then
316
rm --force "$RINGDIR"/mismatch
277
328
fingerprint = $FINGERPRINT
280
331
sed --quiet --expression='
281
332
/^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{