3
3
# Mandos key generator - create new keys for a Mandos client
5
# Copyright © 2008-2019 Teddy Hogeborn
6
# Copyright © 2008-2019 Björn Påhlsson
5
# Copyright © 2008-2018 Teddy Hogeborn
6
# Copyright © 2008-2018 Björn Påhlsson
8
8
# This file is part of Mandos.
147
147
echo "Empty key type" >&2
151
151
if [ -z "$KEYNAME" ]; then
152
152
echo "Empty key name" >&2
156
156
if [ -z "$KEYLENGTH" ] || [ "$KEYLENGTH" -lt 512 ]; then
157
157
echo "Invalid key length" >&2
161
161
if [ -z "$KEYEXPIRE" ]; then
162
162
echo "Empty key expiration" >&2
166
166
# Make FORCE be 0 or 1
168
168
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) FORCE=1;;
169
169
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) FORCE=0;;
172
172
if { [ -e "$SECKEYFILE" ] || [ -e "$PUBKEYFILE" ] \
173
173
|| [ -e "$TLS_PRIVKEYFILE" ] \
174
174
|| [ -e "$TLS_PUBKEYFILE" ]; } \
184
184
if [ -n "$KEYEMAIL" ]; then
185
185
KEYEMAILLINE="Name-Email: $KEYEMAIL"
188
188
# Create temporary gpg batch file
189
189
BATCHFILE="`mktemp -t mandos-keygen-batch.XXXXXXXXXX`"
190
TLS_PRIVKEYTMP="`mktemp -t mandos-keygen-privkey.XXXXXXXXXX`"
193
192
if [ "$mode" = password ]; then
204
203
test -n \"$SECFILE\" && shred --remove \"$SECFILE\"; \
205
test -n \"$TLS_PRIVKEYTMP\" && shred --remove \"$TLS_PRIVKEYTMP\"; \
206
204
shred --remove \"$RINGDIR\"/sec* 2>/dev/null;
207
205
test -n \"$BATCHFILE\" && rm --force \"$BATCHFILE\"; \
208
206
rm --recursive --force \"$RINGDIR\";
243
241
echo -n "Started: "
247
# Generate TLS private key
248
if certtool --generate-privkey --password='' \
249
--outfile "$TLS_PRIVKEYTMP" --sec-param ultra \
250
--key-type="$TLS_KEYTYPE" --pkcs8 --no-text 2>/dev/null; then
252
# Backup any old key files
253
if cp --backup=numbered --force "$TLS_PRIVKEYFILE" "$TLS_PRIVKEYFILE" \
255
shred --remove "$TLS_PRIVKEYFILE" 2>/dev/null || :
245
# Backup any old key files
246
if cp --backup=numbered --force "$TLS_PRIVKEYFILE" "$TLS_PRIVKEYFILE" \
248
shred --remove "$TLS_PRIVKEYFILE"
250
if cp --backup=numbered --force "$TLS_PUBKEYFILE" "$TLS_PUBKEYFILE" \
252
rm --force "$TLS_PUBKEYFILE"
255
## Generate TLS private key
257
# First try certtool from GnuTLS
258
if ! certtool --generate-privkey --password='' \
259
--outfile "$TLS_PRIVKEYFILE" --sec-param ultra \
260
--key-type="$TLS_KEYTYPE" --pkcs8 --no-text 2>/dev/null; then
261
# Otherwise try OpenSSL
262
if ! openssl genpkey -algorithm X25519 -out \
263
/etc/keys/mandos/tls-privkey.pem; then
264
rm --force /etc/keys/mandos/tls-privkey.pem
265
# None of the commands succeded; give up
257
if cp --backup=numbered --force "$TLS_PUBKEYFILE" "$TLS_PUBKEYFILE" \
272
# First try certtool from GnuTLS
273
if ! certtool --password='' --load-privkey="$TLS_PRIVKEYFILE" \
274
--outfile="$TLS_PUBKEYFILE" --pubkey-info --no-text \
276
# Otherwise try OpenSSL
277
if ! openssl pkey -in "$TLS_PRIVKEYFILE" \
278
-out "$TLS_PUBKEYFILE" -pubout; then
259
279
rm --force "$TLS_PUBKEYFILE"
261
cp --archive "$TLS_PRIVKEYTMP" "$TLS_PRIVKEYFILE"
262
shred --remove "$TLS_PRIVKEYTMP" 2>/dev/null || :
266
# First try certtool from GnuTLS
267
if ! certtool --password='' --load-privkey="$TLS_PRIVKEYFILE" \
268
--outfile="$TLS_PUBKEYFILE" --pubkey-info --no-text \
270
# Otherwise try OpenSSL
271
if ! openssl pkey -in "$TLS_PRIVKEYFILE" \
272
-out "$TLS_PUBKEYFILE" -pubout; then
273
rm --force "$TLS_PUBKEYFILE"
274
# None of the commands succeded; give up
280
# None of the commands succeded; give up
280
285
# Make sure trustdb.gpg exists;
281
286
# this is a workaround for Debian bug #737128
282
287
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
287
292
--homedir "$RINGDIR" --trust-model always \
288
293
--gen-key "$BATCHFILE"
289
294
rm --force "$BATCHFILE"
291
296
if tty --quiet; then
292
297
echo -n "Finished: "
296
301
# Backup any old key files
297
302
if cp --backup=numbered --force "$SECKEYFILE" "$SECKEYFILE" \
298
303
2>/dev/null; then
299
shred --remove "$SECKEYFILE" 2>/dev/null || :
304
shred --remove "$SECKEYFILE"
301
306
if cp --backup=numbered --force "$PUBKEYFILE" "$PUBKEYFILE" \
302
307
2>/dev/null; then
303
308
rm --force "$PUBKEYFILE"
306
311
FILECOMMENT="Mandos client key for $KEYNAME"
307
312
if [ "$KEYCOMMENT" != "$KEYCOMMENT_ORIG" ]; then
308
313
FILECOMMENT="$FILECOMMENT ($KEYCOMMENT)"
311
316
if [ -n "$KEYEMAIL" ]; then
312
317
FILECOMMENT="$FILECOMMENT <$KEYEMAIL>"
315
320
# Export key from key rings to key files
316
321
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
317
322
--homedir "$RINGDIR" --armor --export-options export-minimal \
325
330
if [ "$mode" = password ]; then
327
332
# Make SSH be 0 or 1
329
334
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) SSH=1;;
330
335
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|*) SSH=0;;
333
338
if [ $SSH -eq 1 ]; then
334
# The -q option is new in OpenSSH 9.8
335
for ssh_keyscan_quiet in "-q " ""; do
336
for ssh_keytype in ecdsa-sha2-nistp256 ed25519 rsa; do
338
ssh_fingerprint="`ssh-keyscan ${ssh_keyscan_quiet}-t $ssh_keytype localhost 2>/dev/null`"
341
if [ $err -ne 0 ]; then
345
if [ -n "$ssh_fingerprint" ]; then
346
ssh_fingerprint="${ssh_fingerprint#localhost }"
339
for ssh_keytype in ecdsa-sha2-nistp256 ed25519 rsa; do
341
ssh_fingerprint="`ssh-keyscan -t $ssh_keytype localhost 2>/dev/null`"
344
if [ $err -ne 0 ]; then
348
if [ -n "$ssh_fingerprint" ]; then
349
ssh_fingerprint="${ssh_fingerprint#localhost }"
353
355
# Import key into temporary key rings
354
356
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
355
357
--homedir "$RINGDIR" --trust-model always --armor \
357
359
gpg --quiet --batch --no-tty --no-options --enable-dsa2 \
358
360
--homedir "$RINGDIR" --trust-model always --armor \
359
361
--import "$PUBKEYFILE"
361
363
# Get fingerprint of key
362
364
FINGERPRINT="`gpg --quiet --batch --no-tty --no-options \
363
365
--enable-dsa2 --homedir "$RINGDIR" --trust-model always \
364
366
--fingerprint --with-colons \
366
368
--expression='/^fpr:/{s/^fpr:.*:\\([0-9A-Z]*\\):\$/\\1/p;q}'`"
368
370
test -n "$FINGERPRINT"
370
if [ -r "$TLS_PUBKEYFILE" ]; then
371
KEY_ID="$(certtool --key-id --hash=sha256 \
372
KEY_ID="$(certtool --key-id --hash=sha256 \
372
373
--infile="$TLS_PUBKEYFILE" 2>/dev/null || :)"
374
if [ -z "$KEY_ID" ]; then
375
KEY_ID=$(openssl pkey -pubin -in "$TLS_PUBKEYFILE" \
378
| sed --expression='s/^.*[^[:xdigit:]]//')
375
if [ -z "$KEY_ID" ]; then
376
KEY_ID=$(openssl pkey -pubin -in /tmp/tls-pubkey.pem \
379
| sed --expression='s/^.*[^[:xdigit:]]//')
383
383
FILECOMMENT="Encrypted password for a Mandos client"
385
385
while [ ! -s "$SECFILE" ]; do
386
386
if [ -n "$PASSFILE" ]; then
389
389
tty --quiet && stty -echo
390
390
echo -n "Enter passphrase: " >/dev/tty
439
435
if [ -n "$ssh_fingerprint" ]; then
440
echo 'checker = ssh-keyscan '"$ssh_keyscan_quiet"'-t '"$ssh_keytype"' %%(host)s 2>/dev/null | grep --fixed-strings --line-regexp --quiet --regexp=%%(host)s" %(ssh_fingerprint)s"'
436
echo 'checker = ssh-keyscan -t '"$ssh_keytype"' %%(host)s 2>/dev/null | grep --fixed-strings --line-regexp --quiet --regexp=%%(host)s" %(ssh_fingerprint)s"'
441
437
echo "ssh_fingerprint = ${ssh_fingerprint}"
448
444
# Remove the password file, if any
449
445
if [ -n "$SECFILE" ]; then
450
shred --remove "$SECFILE" 2>/dev/null
446
shred --remove "$SECFILE"
452
448
# Remove the key rings
453
449
shred --remove "$RINGDIR"/sec* 2>/dev/null