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"
 
 
43
 
TEMP=`getopt --options vhd:t:l:n:e:c:x:f \
 
44
 
    --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 \
 
45
46
    --name "$0" -- "$@"`
 
 
53
54
       $basename [ OPTIONS ]
 
54
55
   Encrypted password creation:
 
55
56
       $basename { -p | --password } [ --name NAME ] [ --dir DIR]
 
 
57
       $basename { -F | --passfile } FILE [ --name NAME ] [ --dir DIR]
 
57
59
Key creation options:
 
58
60
  -v, --version         Show program's version number and exit
 
59
61
  -h, --help            Show this help message and exit
 
60
62
  -d DIR, --dir DIR     Target directory for key files
 
61
 
  -t TYPE, --type TYPE  Key type.  Default is DSA.
 
 
63
  -t TYPE, --type TYPE  Key type.  Default is RSA.
 
62
64
  -l BITS, --length BITS
 
63
 
                        Key length in bits.  Default is 2048.
 
 
65
                        Key length in bits.  Default is 4096.
 
64
66
  -s TYPE, --subtype TYPE
 
65
 
                        Subkey type.  Default is ELG-E.
 
 
67
                        Subkey type.  Default is RSA.
 
66
68
  -L BITS, --sublength BITS
 
67
 
                        Subkey length in bits.  Default is 2048.
 
 
69
                        Subkey length in bits.  Default is 4096.
 
68
70
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
 
69
71
  -e ADDRESS, --email ADDRESS
 
70
72
                        Email address of key.  Default is empty.
 
71
73
  -c TEXT, --comment TEXT
 
72
 
                        Comment field for key.  The default value is
 
 
74
                        Comment field for key.  The default is empty.
 
74
75
  -x TIME, --expire TIME
 
75
76
                        Key expire time.  Default is no expiration.
 
76
77
                        See gpg(1) for syntax.
 
77
 
  -f, --force           Force overwriting old keys.
 
 
78
  -f, --force           Force overwriting old key files.
 
79
80
Password creation options:
 
80
 
  -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
 
81
86
                        the key directory.  All options other than
 
82
87
                        --dir and --name are ignored.
 
 
89
94
        -p|--password) mode=password; shift;;
 
 
95
        -F|--passfile) mode=password; PASSFILE="$2"; shift 2;;
 
90
96
        -d|--dir) KEYDIR="$2"; shift 2;;
 
91
97
        -t|--type) KEYTYPE="$2"; shift 2;;
 
92
98
        -s|--subtype) SUBKEYTYPE="$2"; shift 2;;
 
 
185
191
shred --remove \"$RINGDIR\"/sec*;
 
186
192
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
 
187
193
rm --recursive --force \"$RINGDIR\";
 
 
194
tty --quiet && stty echo; \
 
193
201
if [ "$mode" = keygen ]; then
 
 
195
203
    cat >"$BATCHFILE" <<-EOF
 
196
204
        Key-Type: $KEYTYPE
 
197
205
        Key-Length: $KEYLENGTH
 
198
 
        #Key-Usage: encrypt,sign,auth
 
199
207
        Subkey-Type: $SUBKEYTYPE
 
200
208
        Subkey-Length: $SUBKEYLENGTH
 
201
 
        #Subkey-Usage: encrypt,sign,auth
 
 
209
        Subkey-Usage: encrypt
 
202
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
 
213
236
    # Generate a new key in the key rings
 
214
237
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
215
238
        --homedir "$RINGDIR" --trust-model always \
 
216
239
        --gen-key "$BATCHFILE"
 
217
240
    rm --force "$BATCHFILE"
 
219
247
    # Backup any old key files
 
220
248
    if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
 
221
249
        2>/dev/null; then
 
 
235
263
        FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
 
238
 
    # Export keys from key rings to key files
 
 
266
    # Export key from key rings to key files
 
239
267
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
240
268
        --homedir "$RINGDIR" --armor --export-options export-minimal \
 
241
269
        --comment "$FILECOMMENT" --output "$SECKEYFILE" \
 
 
248
276
if [ "$mode" = password ]; then
 
249
 
    # Import keys into temporary key rings
 
 
277
    # Import key into temporary key rings
 
250
278
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
251
279
        --homedir "$RINGDIR" --trust-model always --armor \
 
252
280
        --import "$SECKEYFILE"
 
 
266
294
    FILECOMMENT="Encrypted password for a Mandos client"
 
269
 
    echo -n "Enter passphrase: " >&2
 
270
 
    head --lines=1 | tr --delete '\n' \
 
271
 
        | gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
272
 
        --homedir "$RINGDIR" --trust-model always --armor --encrypt \
 
273
 
        --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
 
281
333
        fingerprint = $FINGERPRINT
 
284
336
    sed --quiet --expression='
 
285
337
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{