3
# Mandos key generator - create new OpenPGP keys for Mandos clients
5
# Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
# Contact the authors at <mandos@fukt.bsnet.se>.
26
KEYNAME="`hostname --fqdn`"
28
KEYCOMMENT="Mandos client key"
31
KEYCOMMENT_ORIG="$KEYCOMMENT"
34
TEMP=`getopt --options d:t:l:n:e:c:x:f \
35
--longoptions dir:,type:,length:,name:,email:,comment:,expire:,force \
41
-d|--dir) KEYDIR="$2"; shift 2;;
42
-t|--type) KEYTYPE="$2"; shift 2;;
43
-l|--length) KEYLENGTH="$2"; shift 2;;
44
-n|--name) KEYNAME="$2"; shift 2;;
45
-e|--email) KEYEMAIL="$2"; shift 2;;
46
-c|--comment) KEYCOMMENT="$2"; shift 2;;
47
-x|--expire) KEYCOMMENT="$2"; shift 2;;
48
-f|--force) FORCE=yes; shift;;
50
*) echo "Internal error" >&2; exit 1;;
53
if [ "$#" -gt 0 ]; then
54
echo "Unknown arguments: '$@'" >&2
58
SECKEYFILE="$KEYDIR/seckey.txt"
59
PUBKEYFILE="$KEYDIR/pubkey.txt"
61
# Check for some invalid values
62
if [ -d "$KEYDIR" ]; then :; else
63
echo "$KEYDIR not a directory" >&2
66
if [ -w "$KEYDIR" ]; then :; else
67
echo "Directory $KEYDIR not writeable" >&2
71
if [ -z "$KEYTYPE" ]; then
72
echo "Empty key type" >&2
76
if [ -z "$KEYNAME" ]; then
77
echo "Empty key name" >&2
81
if [ -z "$KEYLENGTH" ] || [ "$KEYLENGTH" -lt 512 ]; then
82
echo "Invalid key length" >&2
86
if [ -z "$KEYEXPIRE" ]; then
87
echo "Empty key expiration" >&2
91
# Make FORCE be 0 or 1
93
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) FORCE=1;;
94
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
97
if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ]; } \
98
&& [ "$FORCE" -eq 0 ]; then
99
echo "Refusing to overwrite old key files; use --force" >&2
103
# Set lines for GPG batch file
104
if [ -n "$KEYCOMMENT" ]; then
105
KEYCOMMENTLINE="Name-Comment: $KEYCOMMENT"
107
if [ -n "$KEYEMAIL" ]; then
108
KEYEMAILLINE="Name-Email: $KEYEMAIL"
112
BATCHFILE="`mktemp -t mandos-gpg-batch.XXXXXXXXXX`"
113
SECRING="`mktemp -t mandos-gpg-secring.XXXXXXXXXX`"
114
PUBRING="`mktemp -t mandos-gpg-pubring.XXXXXXXXXX`"
116
trap "rm --force $PUBRING $BATCHFILE; shred --remove $SECRING" EXIT
118
# Create batch file for GPG
119
cat >"$BATCHFILE" <<EOF
121
Key-Length: $KEYLENGTH
122
#Key-Usage: encrypt,sign,auth
126
Expire-Date: $KEYEXPIRE
133
gpg --no-random-seed-file --quiet --batch --no-tty \
134
--no-default-keyring --batch --secret-keyring "$SECRING" \
135
--keyring "$PUBRING" --gen-key "$BATCHFILE"
136
rm --force "$BATCHFILE"
138
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
140
shred --remove "$SECKEYFILE"
142
if cp --backup=numbered --force "$PUBKEYFILE" "$PUBKEYFILE" \
144
rm --force "$PUBKEYFILE"
147
FILECOMMENT="Mandos client key for $KEYNAME"
148
if [ "$KEYCOMMENT" != "$KEYCOMMENT_ORIG" ]; then
149
FILECOMMENT="$FILECOMMENT ($KEYCOMMENT)"
152
if [ -n "$KEYEMAIL" ]; then
153
FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
156
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
157
--no-default-keyring --secret-keyring "$SECRING" \
158
--keyring "$PUBRING" --export-options export-minimal \
159
--comment "$FILECOMMENT" --output "$SECKEYFILE" \
161
gpg --no-random-seed-file --quiet --batch --no-tty --armor \
162
--no-default-keyring --secret-keyring "$SECRING" \
163
--keyring "$PUBRING" --export-options export-minimal \
164
--comment "$FILECOMMENT" --output "$PUBKEYFILE" \