1+ #! /bin/sh
2+ image_file=" ${1} "
3+ cms_sig_file=" sig.cms"
4+ lines_for_lookup=50
5+ SECURE_UPGRADE_ENABLED=0
6+ DIR=" $( dirname " $0 " ) "
7+ if [ -d " /sys/firmware/efi/efivars" ]; then
8+ if ! [ -n " $( ls -A /sys/firmware/efi/efivars 2> /dev/null) " ]; then
9+ mount -t efivarfs none /sys/firmware/efi/efivars 2> /dev/null
10+ fi
11+ SECURE_UPGRADE_ENABLED=$( bootctl status 2> /dev/null | grep -c " Secure Boot: enabled" )
12+ else
13+ echo " efi not supported - exiting without verification"
14+ exit 0
15+ fi
16+
17+ . /usr/local/bin/verify_image_sign_common.sh
18+
19+ if [ ${SECURE_UPGRADE_ENABLED} -eq 0 ]; then
20+ echo " secure boot not enabled - exiting without image verification"
21+ exit 0
22+ fi
23+
24+ clean_up ()
25+ {
26+ if [ -d ${EFI_CERTS_DIR} ]; then rm -rf ${EFI_CERTS_DIR} ; fi
27+ if [ -d " ${TMP_DIR} " ]; then rm -rf ${TMP_DIR} ; fi
28+ exit $1
29+ }
30+
31+ TMP_DIR=$( mktemp -d)
32+ DATA_FILE=" ${TMP_DIR} /data.bin"
33+ CMS_SIG_FILE=" ${TMP_DIR} /${cms_sig_file} "
34+ TAR_SIZE=$( head -n $lines_for_lookup $image_file | grep " payload_image_size=" | cut -d" =" -f2- )
35+ SHARCH_SIZE=$( sed ' /^exit_marker$/q' $image_file | wc -c)
36+ SIG_PAYLOAD_SIZE=$(( $TAR_SIZE + $SHARCH_SIZE ))
37+ # Extract cms signature from signed file
38+ # Add extra byte for payload
39+ sed -e ' 1,/^exit_marker$/d' $image_file | tail -c +$(( $TAR_SIZE + 1 )) > $CMS_SIG_FILE
40+ # Extract image from signed file
41+ head -c $SIG_PAYLOAD_SIZE $image_file > $DATA_FILE
42+ # verify signature with certificate fetched with efi tools
43+ EFI_CERTS_DIR=/tmp/efi_certs
44+ [ -d $EFI_CERTS_DIR ] && rm -rf $EFI_CERTS_DIR
45+ mkdir $EFI_CERTS_DIR
46+ efi-readvar -v db -o $EFI_CERTS_DIR /db_efi > /dev/null ||
47+ {
48+ echo " Error: unable to read certs from efi db: $? "
49+ clean_up 1
50+ }
51+ # Convert one file to der certificates
52+ sig-list-to-certs $EFI_CERTS_DIR /db_efi $EFI_CERTS_DIR /db > /dev/null||
53+ {
54+ echo " Error: convert sig list to certs: $? "
55+ clean_up 1
56+ }
57+ for file in $( ls $EFI_CERTS_DIR | grep " db-" ) ; do
58+ LOG=$( openssl x509 -in $EFI_CERTS_DIR /$file -inform der -out $EFI_CERTS_DIR /cert.pem 2>&1 )
59+ if [ $? -ne 0 ]; then
60+ logger " cms_validation: $LOG "
61+ fi
62+ # Verify detached signature
63+ LOG=$( verify_image_sign_common $image_file $DATA_FILE $CMS_SIG_FILE )
64+ VALIDATION_RES=$?
65+ if [ $VALIDATION_RES -eq 0 ]; then
66+ RESULT=" CMS Verified OK using efi keys"
67+ echo " verification ok:$RESULT "
68+ # No need to continue.
69+ # Exit without error if any success signature verification.
70+ clean_up 0
71+ fi
72+ done
73+ echo " Failure: CMS signature Verification Failed: $LOG "
74+
75+ clean_up 1
0 commit comments