/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-keygen

  • Committer: Teddy Hogeborn
  • Date: 2008-09-13 15:36:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080913153618-atp386q2bqj0ku99
* Makefile (install-client-nokey): Do "&&" instead of ";" to catch
                                   errors.

* README: Kill the straight quotes.  Add copyright notice.

* overview.xml: Improved wording.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
3
3
# Mandos key generator - create a new OpenPGP key for a Mandos client
4
4
5
 
# Copyright © 2008-2014 Teddy Hogeborn
6
 
# Copyright © 2008-2014 Björn Påhlsson
 
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
6
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/>.
20
19
21
 
# Contact the authors at <mandos@recompile.se>.
 
20
# Contact the authors at <mandos@fukt.bsnet.se>.
22
21
23
22
 
24
 
VERSION="1.6.4"
 
23
VERSION="1.0"
25
24
 
26
25
KEYDIR="/etc/keys/mandos"
27
 
KEYTYPE=RSA
28
 
KEYLENGTH=4096
29
 
SUBKEYTYPE=RSA
30
 
SUBKEYLENGTH=4096
31
 
KEYNAME="`hostname --fqdn 2>/dev/null || hostname`"
 
26
KEYTYPE=DSA
 
27
KEYLENGTH=2048
 
28
SUBKEYTYPE=ELG-E
 
29
SUBKEYLENGTH=2048
 
30
KEYNAME="`hostname --fqdn`"
32
31
KEYEMAIL=""
33
 
KEYCOMMENT=""
 
32
KEYCOMMENT="Mandos client key"
34
33
KEYEXPIRE=0
35
34
FORCE=no
36
35
KEYCOMMENT_ORIG="$KEYCOMMENT"
41
40
fi
42
41
 
43
42
# Parse options
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" -- "$@"`
47
46
 
48
47
help(){
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]
58
56
 
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
 
73
                        "Mandos client key".
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.
79
78
 
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.
88
83
EOF
92
87
while :; do
93
88
    case "$1" in
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;;
146
140
        echo "Invalid key length" >&2
147
141
        exit 1
148
142
    fi
149
 
    
 
143
 
150
144
    if [ -z "$KEYEXPIRE" ]; then
151
145
        echo "Empty key expiration" >&2
152
146
        exit 1
171
165
    if [ -n "$KEYEMAIL" ]; then
172
166
        KEYEMAILLINE="Name-Email: $KEYEMAIL"
173
167
    fi
174
 
    
 
168
 
175
169
    # Create temporary gpg batch file
176
170
    BATCHFILE="`mktemp -t mandos-keygen-batch.XXXXXXXXXX`"
177
171
fi
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; \
 
188
stty echo; \
195
189
" EXIT
196
190
 
197
 
set -e
198
 
 
199
191
umask 077
200
192
 
201
193
if [ "$mode" = keygen ]; then
203
195
    cat >"$BATCHFILE" <<-EOF
204
196
        Key-Type: $KEYTYPE
205
197
        Key-Length: $KEYLENGTH
206
 
        Key-Usage: sign,auth
 
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
211
203
        $KEYCOMMENTLINE
212
204
        $KEYEMAILLINE
218
210
        %commit
219
211
        EOF
220
212
    
221
 
    if tty --quiet; then
222
 
        cat <<-EOF
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.
226
 
        EOF
227
 
        echo -n "Started: "
228
 
        date
229
 
    fi
230
 
    
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"
241
218
    
242
 
    if tty --quiet; then
243
 
        echo -n "Finished: "
244
 
        date
245
 
    fi
246
 
    
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>"
264
236
    fi
265
237
    
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" \
274
246
fi
275
247
 
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"
293
265
    
294
266
    FILECOMMENT="Encrypted password for a Mandos client"
295
267
    
296
 
    while [ ! -s "$SECFILE" ]; do
297
 
        if [ -n "$PASSFILE" ]; then
298
 
            cat "$PASSFILE"
299
 
        else
300
 
            tty --quiet && stty -echo
301
 
            echo -n "Enter passphrase: " >&2
302
 
            read first
303
 
            tty --quiet && echo >&2
304
 
            echo -n "Repeat passphrase: " >&2
305
 
            read second
306
 
            if tty --quiet; then
307
 
                echo >&2
308
 
                stty echo
309
 
            fi
310
 
            if [ "$first" != "$second" ]; then
311
 
                echo "Passphrase mismatch" >&2
312
 
                touch "$RINGDIR"/mismatch
313
 
            else
314
 
                echo -n "$first"
315
 
            fi
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
322
 
            if tty --quiet; then
323
 
                > "$SECFILE"
324
 
            else
325
 
                exit 1
326
 
            fi
327
 
        fi
328
 
    done
 
268
    stty -echo
 
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" \
 
274
        > "$SECFILE"
 
275
    echo >&2
 
276
    stty echo
329
277
    
330
278
    cat <<-EOF
331
279
        [$KEYNAME]
332
280
        host = $KEYNAME
333
281
        fingerprint = $FINGERPRINT
334
282
        secret =
335
 
        EOF
 
283
EOF
336
284
    sed --quiet --expression='
337
285
        /^-----BEGIN PGP MESSAGE-----$/,/^-----END PGP MESSAGE-----$/{
338
286
            /^$/,${