| 1 |
#! /bin/sh |
| 2 |
|
| 3 |
# A script for upgrading lsh private keys |
| 4 |
|
| 5 |
werror () { |
| 6 |
echo "$1" >&2 |
| 7 |
} |
| 8 |
|
| 9 |
die () { |
| 10 |
werror "$1" |
| 11 |
exit 1 |
| 12 |
} |
| 13 |
|
| 14 |
if [ $# -eq 0 ] ; then |
| 15 |
werror "You must supply a key to update, the upgraded key will" |
| 16 |
werror "have the suffix .new." |
| 17 |
werror "" |
| 18 |
werror "Usage: key.." |
| 19 |
exit 1 |
| 20 |
fi |
| 21 |
|
| 22 |
umask 077 |
| 23 |
|
| 24 |
: ${SEXP_CONV:=sexp-conv} |
| 25 |
: ${LSH_DECRYPT_KEY:=lsh-decrypt-key} |
| 26 |
: ${LSH_WRITEKEY:=lsh-writekey} |
| 27 |
|
| 28 |
type "$SEXP_CONV" >/dev/null 2>&1 || die "Can't find the sexp-conv program" |
| 29 |
type "$LSH_DECRYPT_KEY" >/dev/null 2>&1 || die "Can't find the lsh-decrypt-key program" |
| 30 |
type "$LSH_WRITEKEY" >/dev/null 2>&1 || die "Can't find the lsh-writekey program" |
| 31 |
|
| 32 |
for p in $@; do |
| 33 |
werror "Converting key $p" |
| 34 |
|
| 35 |
|
| 36 |
# These are the changes we must make: |
| 37 |
# |
| 38 |
# * Numbers are signed, so the most significant bit of all our |
| 39 |
# numbers must be 0. So we add a leading zero octet to numbers |
| 40 |
# that need it. |
| 41 |
|
| 42 |
# It also seems we must reconvert back to transport format to make lsh-writekey |
| 43 |
|
| 44 |
|
| 45 |
if "$SEXP_CONV" -s advanced < "$p" \ |
| 46 |
| grep 'password-encrypted' >/dev/null; then |
| 47 |
werror "Key is encrypted and must be decrypted." |
| 48 |
|
| 49 |
# Encrypted key |
| 50 |
if "$LSH_DECRYPT_KEY" --in="$p"; then \ |
| 51 |
werror "Key will be reencrypted using aes256-cbc"; \ |
| 52 |
else \ |
| 53 |
werror "Decryption failed for $p, aborting."; \ |
| 54 |
die "(errors from lsh-writekey may be ignored)."; \ |
| 55 |
fi | \ |
| 56 |
"$SEXP_CONV" -s hex \ |
| 57 |
| sed -e 's,(\(.\) #\([89a-fA-F]\),(\1 #00\2,' \ |
| 58 |
| "$SEXP_CONV" -s transport \ |
| 59 |
| "$LSH_WRITEKEY" -c aes256-cbc -o "$p.new" |
| 60 |
else |
| 61 |
# Not encrypted |
| 62 |
"$SEXP_CONV" -s hex <"$p" \ |
| 63 |
| sed -e 's,(\(.\) #\([89a-fA-F]\),(\1 #00\2,' \ |
| 64 |
| "$SEXP_CONV" -s transport \ |
| 65 |
| "$LSH_WRITEKEY" -o "$p.new" |
| 66 |
|
| 67 |
fi |
| 68 |
done |