Skip to content

Commit 43585c3

Browse files
Add support for secure upgrade (#11862) (#15691)
- What I did Added support for secure upgrade. - How I did it During sonic_installer install, added secure upgrade image verification. HLD can be found in the following PR: sonic-net/SONiC#1024 - Why I did it Feature is used to allow image was not modified since built from vendor. During installation, image can be verified with a signature attached to it. - How I did it Feature includes image signing during build (in sonic buildimage repo) and verification during image install (in sonic-utilities). - How to verify it In order for image verification - image must be signed - need to provide signing key and certificate (paths in SECURE_UPGRADE_DEV_SIGNING_KEY and SECURE_UPGRADE_DEV_SIGNING_CERT in rules/config) during build , and during image install, need to enable secure boot flag in bios, and signing_certificate should be available in bios. - Feature dependencies In order for this feature to work smoothly, need to have secure boot feature implemented as well. The Secure boot feature will be merged in the near future. Co-authored-by: ycoheNvidia <99744138+ycoheNvidia@users.noreply.github.com>
1 parent 8c94e32 commit 43585c3

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

build_image.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ generate_onie_installer_image()
8686
## Note: Don't leave blank between lines. It is single line command.
8787
./onie-mk-demo.sh $CONFIGURED_ARCH $TARGET_MACHINE $TARGET_PLATFORM-$TARGET_MACHINE-$ONIEIMAGE_VERSION \
8888
installer platform/$TARGET_MACHINE/platform.conf $output_file OS $IMAGE_VERSION $ONIE_IMAGE_PART_SIZE \
89-
$ONIE_INSTALLER_PAYLOAD
89+
$ONIE_INSTALLER_PAYLOAD $SECURE_UPGRADE_SIGNING_CERT $SECURE_UPGRADE_DEV_SIGNING_KEY
9090
}
9191

9292
# Generate asic-specific device list

installer/sharch_body.sh

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
##
1212

1313
echo -n "Verifying image checksum ..."
14-
sha1=$(sed -e '1,/^exit_marker$/d' "$0" | sha1sum | awk '{ print $1 }')
14+
payload_image_size=%%PAYLOAD_IMAGE_SIZE%%
15+
16+
sha1=$(sed -e '1,/^exit_marker$/d' "$0" | head -c $payload_image_size | sha1sum | awk '{ print $1 }')
1517

1618
payload_sha1=%%IMAGE_SHA1%%
1719

@@ -45,7 +47,9 @@ if [ "$(id -u)" = "0" ] ; then
4547
fi
4648
cd $tmp_dir
4749
echo -n "Preparing image archive ..."
48-
sed -e '1,/^exit_marker$/d' $archive_path | tar xf - || exit 1
50+
51+
sed -e '1,/^exit_marker$/d' $archive_path | head -c $payload_image_size | tar xf - || exit 1
52+
4953
echo " OK."
5054
cd $cur_wd
5155
if [ -n "$extract" ] ; then

onie-mk-demo.sh

+47-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ output_file=$6
1414
demo_type=$7
1515
image_version=$8
1616
onie_image_part_size=$9
17+
onie_installer_payload=${10}
18+
cert_file=${11}
19+
key_file=${12}
1720

1821
shift 9
1922

@@ -100,7 +103,7 @@ sed -i -e "s/%%DEMO_TYPE%%/$demo_type/g" \
100103
-e "s@%%OUTPUT_RAW_IMAGE%%@$output_raw_image@" \
101104
$tmp_installdir/install.sh || clean_up 1
102105
echo -n "."
103-
cp -r $* $tmp_installdir || clean_up 1
106+
cp -r $onie_installer_payload $tmp_installdir || clean_up 1
104107
echo -n "."
105108
[ -r "$platform_conf" ] && {
106109
cp $platform_conf $tmp_installdir || clean_up 1
@@ -130,7 +133,50 @@ cp $installer_dir/sharch_body.sh $output_file || {
130133
# Replace variables in the sharch template
131134
sed -i -e "s/%%IMAGE_SHA1%%/$sha1/" $output_file
132135
echo -n "."
136+
tar_size="$(wc -c < "${sharch}")"
137+
sed -i -e "s|%%PAYLOAD_IMAGE_SIZE%%|${tar_size}|" ${output_file}
133138
cat $sharch >> $output_file
139+
echo "secure upgrade flags: SECURE_UPGRADE_MODE = $SECURE_UPGRADE_MODE, \
140+
SECURE_UPGRADE_DEV_SIGNING_KEY = $SECURE_UPGRADE_DEV_SIGNING_KEY, SECURE_UPGRADE_SIGNING_CERT = $SECURE_UPGRADE_SIGNING_CERT"
141+
142+
if [ "$SECURE_UPGRADE_MODE" = "dev" -o "$SECURE_UPGRADE_MODE" = "prod" ]; then
143+
CMS_SIG="${tmp_dir}/signature.sig"
144+
DIR="$(dirname "$0")"
145+
scripts_dir="${DIR}/scripts"
146+
echo "$0 $SECURE_UPGRADE_MODE signing - creating CMS signature for ${output_file}. Output file ${CMS_SIG}"
147+
148+
if [ "$SECURE_UPGRADE_MODE" = "dev" ]; then
149+
echo "$0 dev keyfile location: ${key_file}."
150+
[ -f ${scripts_dir}/sign_image_dev.sh ] || {
151+
echo "dev sign script ${scripts_dir}/sign_image_dev.sh not found"
152+
rm -rf ${output_file}
153+
}
154+
(${scripts_dir}/sign_image_dev.sh ${cert_file} ${key_file} ${output_file} ${CMS_SIG}) || {
155+
echo "CMS sign error $?"
156+
rm -rf ${CMS_SIG} ${output_file}
157+
}
158+
else # "$SECURE_UPGRADE_MODE" has to be equal to "prod"
159+
[ -f ${scripts_dir}/sign_image_${machine}.sh ] || {
160+
echo "prod sign script ${scripts_dir}/sign_image_${machine}.sh not found"
161+
rm -rf ${output_file}
162+
}
163+
(${scripts_dir}/sign_image_${machine}.sh ${output_file} ${CMS_SIG} ${SECURE_UPGRADE_MODE}) || {
164+
echo "CMS sign error $?"
165+
rm -rf ${CMS_SIG} ${output_file}
166+
}
167+
fi
168+
169+
[ -f "$CMS_SIG" ] || {
170+
echo "Error: CMS signature not created - exiting without signing"
171+
clean_up 1
172+
}
173+
# append signature to binary
174+
cat ${CMS_SIG} >> ${output_file}
175+
sudo rm -rf ${CMS_SIG}
176+
elif [ "$SECURE_UPGRADE_MODE" -ne "no_sign" ]; then
177+
echo "SECURE_UPGRADE_MODE not defined or defined as $SECURE_UPGRADE_MODE - build without signing"
178+
fi
179+
134180
rm -rf $tmp_dir
135181
echo " Done."
136182

scripts/sign_image_dev.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cert_file=$1
2+
key_file=$2
3+
image_to_sign=$3
4+
cms_sig_out=$4
5+
openssl cms -sign -nosmimecap -signer ${cert_file} -inkey ${key_file} -binary -in $image_to_sign -outform pem -out ${cms_sig_out} || {
6+
echo "$?: CMS sign error"
7+
sudo rm -rf ${cms_sig_out}
8+
exit 1
9+
}
10+
echo "CMS sign OK"
11+
exit 0

0 commit comments

Comments
 (0)