3
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
 
5
 
# Copyright © 2008-2013 Teddy Hogeborn
 
6
 
# Copyright © 2008-2013 Björn Påhlsson
 
 
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
8
7
# This program is free software: you can redistribute it and/or modify
 
9
8
# it under the terms of the GNU General Public License as published by
 
 
18
17
# You should have received a copy of the GNU General Public License
 
19
18
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 
# Contact the authors at <mandos@recompile.se>.
 
 
20
# Contact the authors at <mandos@fukt.bsnet.se>.
 
26
25
KEYDIR="/etc/keys/mandos"
 
31
30
KEYNAME="`hostname --fqdn 2>/dev/null || hostname`"
 
 
32
KEYCOMMENT="Mandos client key"
 
36
35
KEYCOMMENT_ORIG="$KEYCOMMENT"
 
 
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 \
 
 
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 \
 
46
45
    --name "$0" -- "$@"`
 
 
54
53
       $basename [ OPTIONS ]
 
55
54
   Encrypted password creation:
 
56
55
       $basename { -p | --password } [ --name NAME ] [ --dir DIR]
 
57
 
       $basename { -F | --passfile } FILE [ --name NAME ] [ --dir DIR]
 
59
57
Key creation options:
 
60
58
  -v, --version         Show program's version number and exit
 
61
59
  -h, --help            Show this help message and exit
 
62
60
  -d DIR, --dir DIR     Target directory for key files
 
63
 
  -t TYPE, --type TYPE  Key type.  Default is RSA.
 
 
61
  -t TYPE, --type TYPE  Key type.  Default is DSA.
 
64
62
  -l BITS, --length BITS
 
65
 
                        Key length in bits.  Default is 4096.
 
 
63
                        Key length in bits.  Default is 2048.
 
66
64
  -s TYPE, --subtype TYPE
 
67
 
                        Subkey type.  Default is RSA.
 
 
65
                        Subkey type.  Default is ELG-E.
 
68
66
  -L BITS, --sublength BITS
 
69
 
                        Subkey length in bits.  Default is 4096.
 
 
67
                        Subkey length in bits.  Default is 2048.
 
70
68
  -n NAME, --name NAME  Name of key.  Default is the FQDN.
 
71
69
  -e ADDRESS, --email ADDRESS
 
72
70
                        Email address of key.  Default is empty.
 
73
71
  -c TEXT, --comment TEXT
 
74
 
                        Comment field for key.  The default is empty.
 
 
72
                        Comment field for key.  The default value is
 
75
74
  -x TIME, --expire TIME
 
76
75
                        Key expire time.  Default is no expiration.
 
77
76
                        See gpg(1) for syntax.
 
78
 
  -f, --force           Force overwriting old key files.
 
 
77
  -f, --force           Force overwriting old keys.
 
80
79
Password creation options:
 
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
 
 
80
  -p, --password        Create an encrypted password using the keys in
 
86
81
                        the key directory.  All options other than
 
87
82
                        --dir and --name are ignored.
 
 
94
89
        -p|--password) mode=password; shift;;
 
95
 
        -F|--passfile) mode=password; PASSFILE="$2"; shift 2;;
 
96
90
        -d|--dir) KEYDIR="$2"; shift 2;;
 
97
91
        -t|--type) KEYTYPE="$2"; shift 2;;
 
98
92
        -s|--subtype) SUBKEYTYPE="$2"; shift 2;;
 
 
191
185
shred --remove \"$RINGDIR\"/sec*;
 
192
186
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
 
193
187
rm --recursive --force \"$RINGDIR\";
 
194
 
tty --quiet && stty echo; \
 
201
193
if [ "$mode" = keygen ]; then
 
 
203
195
    cat >"$BATCHFILE" <<-EOF
 
204
196
        Key-Type: $KEYTYPE
 
205
197
        Key-Length: $KEYLENGTH
 
 
198
        #Key-Usage: encrypt,sign,auth
 
207
199
        Subkey-Type: $SUBKEYTYPE
 
208
200
        Subkey-Length: $SUBKEYLENGTH
 
209
 
        Subkey-Usage: encrypt
 
 
201
        #Subkey-Usage: encrypt,sign,auth
 
210
202
        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
 
236
213
    # Generate a new key in the key rings
 
237
214
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
238
215
        --homedir "$RINGDIR" --trust-model always \
 
239
216
        --gen-key "$BATCHFILE"
 
240
217
    rm --force "$BATCHFILE"
 
247
219
    # Backup any old key files
 
248
220
    if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
 
249
221
        2>/dev/null; then
 
 
263
235
        FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
 
266
 
    # Export key from key rings to key files
 
 
238
    # Export keys from key rings to key files
 
267
239
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
268
240
        --homedir "$RINGDIR" --armor --export-options export-minimal \
 
269
241
        --comment "$FILECOMMENT" --output "$SECKEYFILE" \
 
 
276
248
if [ "$mode" = password ]; then
 
277
 
    # Import key into temporary key rings
 
 
249
    # Import keys into temporary key rings
 
278
250
    gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
 
279
251
        --homedir "$RINGDIR" --trust-model always --armor \
 
280
252
        --import "$SECKEYFILE"
 
 
294
266
    FILECOMMENT="Encrypted password for a Mandos client"
 
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
 
 
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" \
 
333
281
        fingerprint = $FINGERPRINT
 
336
284
    sed --quiet --expression='
 
337
285
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{