|
| 1 | +#! /bin/sh |
| 2 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | +# |
| 4 | +# check for MD5 computation errors from "firmwarecfg", depending on size of data to |
| 5 | +# sign |
| 6 | +# |
| 7 | +# the script needs full-functioning signing and verification from the "signimage" |
| 8 | +# folder and the own public key as "~/firmware_signing.asc" |
| 9 | +# |
| 10 | +# specify the password for the private key as first parameter |
| 11 | +# |
| 12 | +# the script will create image files with one, two and 512 blocks with 4 KB of data |
| 13 | +# to sign and appends from 0 to 7 further blocks, to check every possible count of |
| 14 | +# remaining TAR blocks after the last complete 4 KB block |
| 15 | +# |
| 16 | +# each signed image is tested afterwards - once with the "check_signed_image" script |
| 17 | +# and once more with AVM's tr069fwupdate utility |
| 18 | +# |
| 19 | +# You need an OpenSSL CLI utility and the "hexdump" applet as additions to the |
| 20 | +# original firmware from AVM to run this script successfully. |
| 21 | +# |
| 22 | +check() |
| 23 | +{ |
| 24 | + check_signed_image $1 -a ~/firmware_signing.asc |
| 25 | +} |
| 26 | +sign() |
| 27 | +( |
| 28 | + [ -z "$3" ] && export YF_SIGNIMAGE_SKIP_WORKAROUNDS=1 || export -n YF_SIGNIMAGE_SKIP_WORKAROUNDS |
| 29 | + sign_image "$1" "$2" |
| 30 | +) |
| 31 | +pack() |
| 32 | +{ |
| 33 | + tar -c "$*" |
| 34 | +} |
| 35 | +show() |
| 36 | +{ |
| 37 | + size=$(stat -c %s "$1") |
| 38 | + off=$(hexdump -C "$1" | grep "\./var/signature" | sed -n -e "s|^\([^ ]*\) .*\$|\1|p") |
| 39 | + sizediff=1024 |
| 40 | + [ "$2" = "Signed" ] && sizediff=$(( sizediff + 1024 )) |
| 41 | + printf "%s file: size=%d (0x%08x), data to sign=%d (0x%08x)" "$2" $size $size $(( size - sizediff )) $(( size - sizediff )) |
| 42 | + [ -z $off ] || printf ", signature offset=%s (remainder: %u)" $off $(( ( ( 0x$off - 1024 ) % 4096 ) / 512 )) |
| 43 | + printf ", TAR blocks remainder - 4K: \033[36m%u\033[0m, 10K: \033[36m%u\033[0m\n" $(( ( ( size - sizediff ) % 4096 ) / 512 )) $(( ( ( size - sizediff ) % 10240 ) / 512 )) |
| 44 | +} |
| 45 | +hash() |
| 46 | +{ |
| 47 | + size=$(stat -c %s "$1") |
| 48 | + blocks=$(( size / 512 )) |
| 49 | + md5=$( ( dd if="$1" bs=512 count=$(( blocks - 4 )) 2>/dev/null;dd if=/dev/zero bs=512 count=4 2>/dev/null ) | md5sum | sed -n -e "s|^\([a-f0-9]*\).*\$|\1|p") |
| 50 | + printf "Hash value: %s\n" $md5 |
| 51 | +} |
| 52 | +run() |
| 53 | +{ |
| 54 | + printf "\033[1m\033[31mi=%u j=%u %s\033[0m\n" "$1" "$2" "$([ -n "$3" ] && printf "with workaround while signing")" |
| 55 | + rm -r /var/packet 2>/dev/null |
| 56 | + dir="$TMPDIR/test_signature" |
| 57 | + rm -r "$dir" 2>/dev/null |
| 58 | + mkdir -p "$dir/var" |
| 59 | + dd if=/dev/zero of="$dir/var/zeros.bin" bs=512 count=$(( ( $1 * 8 ) - 6 + $2 )) 2>/dev/null |
| 60 | + cat >"$dir/var/install" <<EOF |
| 61 | +#! /bin/sh |
| 62 | +printf "\033[1m/var/install from signed image file\n======================\n" 1>&2 |
| 63 | +printf "\033[1mfirmware_stream_result:\033[0m\n----------------------\n" 1>&2 |
| 64 | +cat /var/tmp/firmware_stream_result 1>&2 |
| 65 | +printf "\033[1mfwsign.log:\033[0m\n----------\n" 1>&2 |
| 66 | +cat /var/tmp/fwsign.log 1>&2 |
| 67 | +exit 0 |
| 68 | +EOF |
| 69 | + chmod a+x "$dir/var/install" |
| 70 | + cd "$dir" |
| 71 | + pack "./var/" > "$dir/$i.image" |
| 72 | + show "$dir/$i.image" "Unsigned" |
| 73 | + sign "$dir/$i.image" "$keypwd" "$3" > "$dir/$i.signed.image" 2>/dev/null |
| 74 | + show "$dir/$i.signed.image" "Signed" |
| 75 | + check "$dir/$i.signed.image" 2>/dev/null |
| 76 | + yf_check=$? |
| 77 | + hash "$dir/$i.signed.image" 2>/dev/null |
| 78 | + tr069fwupdate packet "file://$(realpath "$dir")/$i.signed.image" 2>/dev/null |
| 79 | + avm_check=$? |
| 80 | + [ $avm_check -eq 0 ] && echo "AVM signature check: $avm_check, YourFritz signature check: $yf_check" || echo -e "\033[1m\033[31mAVM signature check: $avm_check\033[0m, YourFritz signature check: $yf_check \033[31m\033[1mwithout workarounds while signing\033[0m\n" |
| 81 | + if [ $avm_check -ne 0 ]; then |
| 82 | + printf "\033[1mfirmware_stream_result\033[0m\n======================\n" |
| 83 | + cat /var/tmp/firmware_stream_result 2>/dev/null |
| 84 | + printf "\033[1mfwsign.log\033[0m\n==========\n" |
| 85 | + head -n 2 /var/tmp/fwsign.log 2>/dev/null |
| 86 | + fi |
| 87 | + cd /var |
| 88 | + rm -r "$dir" 2>/dev/null |
| 89 | + printf "\n------------------------------------------------\n\n" |
| 90 | + [ "$avm_check" -ne 0 ] && [ -z "$3" ] && run $1 $2 1 |
| 91 | +} |
| 92 | +keypwd="$1" |
| 93 | +mount -o bind ~/firmware_signing.asc /etc/avm_firmware_public_key1 |
| 94 | +for i in 1 2 512; do |
| 95 | + for j in $(seq 8); do |
| 96 | + run $i $(( j - 1 )) |
| 97 | + done |
| 98 | +done |
| 99 | +while umount /etc/avm_firmware_public_key1 2>/dev/null; do :; done |
0 commit comments