Fix bugs in kem-import script.

pull/925/head
Fredrik Öhrström 2023-03-22 11:03:57 +01:00
rodzic 0297e7c329
commit 3a97c9a453
1 zmienionych plików z 28 dodań i 11 usunięć

Wyświetl plik

@ -12,11 +12,9 @@ if unzip -v "$file" > /dev/null 2>&1
then
rm -rf temp_extracting_kem
mkdir temp_extracting_kem
unzip -d temp_extracting_kem "$file"
unzip -q -d temp_extracting_kem "$file"
file=$(echo temp_extracting_kem/*.kem)
if [ ! -f "$file" ]; then file=$(echo temp_extracting_kem/*.kem2) ; fi
echo "Extracting from $file"
echo
fi
# The key is the supplied password as raw bytes padded with zero bytes.
@ -24,19 +22,38 @@ fi
key=$(echo -n "$password" | xxd -p)
key=$(printf "%-32s" "$key" | tr ' ' '0')
echo "KEY=>$key<"
xml=$(cat "$file")
# Extract the Base64-encoded ciphertext from the XML
b64=$(echo "$xml" | sed -n 's/.*<CipherValue>\(.*\)<\/CipherValue>.*/\1/p' > gurka)
b64=$(echo "$xml" | sed -n 's/.*<CipherValue>\(.*\)<\/CipherValue>.*/\1/p')
echo BASE64="$b64"
# Decrypt the ciphertext using the key and IV
plain=$(echo "$b64" | base64 -d | openssl enc -aes-128-cbc -d -K "$key" -iv "$key" -nopad)
# Extract the id and key from the decrypted XML
meter_key=$(echo "$plain" | sed -n 's/.*<DEK>\(.*\)<\/DEK>.*/\1/p')
meter_id=$(echo "$plain" | sed -n 's/.*<SerialNo>\(.*\)<\/SerialNo>.*/\1/p')
good=false
echo "id=$meter_id"
echo "key=$meter_key"
if echo "$plain" | grep -q "<Name>DEK</Name><Value>"
then
# kem2
meter_key=$(echo "$plain" | sed -n 's/.*<Name>DEK<\/Name><Value>\(.*\)<\/Value>.*/\1/p')
meter_id=$(echo "$plain" | sed -n 's/.*<SerialNumber>\(.*\)<\/SerialNumber>.*/\1/p')
good=true
fi
if echo "$plain" | grep -q "<DEK>"
then
# kem
meter_key=$(echo "$plain" | sed -n 's/.*<DEK>\(.*\)<\/DEK>.*/\1/p')
meter_id=$(echo "$plain" | sed -n 's/.*<SerialNo>\(.*\)<\/SerialNo>.*/\1/p')
good=true
fi
if [ "$good" = true ]
then
echo "meter id: $meter_id"
echo "meter key: $meter_key"
rm -rf temp_extracting_kem
else
echo "Error when extracting. To debug run: bash -x kem-extract $*"
echo "Collect the output and create an issue at https://github.com/wmbusmeters/wmbusmeters"
fi