Skip to content

Commit 59efea1

Browse files
[202211_SPC4] Secure upgrade (#39)
* Implemented secure upgrade * Fixed weird binary related sha1 issue * Fixed sha1 installer issue * Fixed minor sharch_body.sh issue * minor typo fix * newline * fixed secure upgrade image creation bug copying irrelevant files to image * Improved prints and remove image on build failure --------- Co-authored-by: Yona Cohen <[email protected]> Co-authored-by: ycoheNvidia <[email protected]>
1 parent 9f53d9a commit 59efea1

5 files changed

Lines changed: 78 additions & 4 deletions

File tree

Makefile.work

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ SONIC_BUILD_INSTRUCTION := $(MAKE) \
518518
SECURE_UPGRADE_PROD_SIGNING_TOOL=$(SECURE_UPGRADE_PROD_SIGNING_TOOL) \
519519
SECURE_UPGRADE_PROD_TOOL_ARGS=$(SECURE_UPGRADE_PROD_TOOL_ARGS) \
520520
SONIC_DEFAULT_CONTAINER_REGISTRY=$(DEFAULT_CONTAINER_REGISTRY) \
521+
SECURE_UPGRADE_MODE=$(SECURE_UPGRADE_MODE) \
522+
SECURE_UPGRADE_DEV_SIGNING_KEY=$(SECURE_UPGRADE_DEV_SIGNING_KEY) \
523+
SECURE_UPGRADE_DEV_SIGNING_CERT=$(SECURE_UPGRADE_DEV_SIGNING_CERT) \
521524
ENABLE_HOST_SERVICE_ON_START=$(ENABLE_HOST_SERVICE_ON_START) \
522525
SLAVE_DIR=$(SLAVE_DIR) \
523526
ENABLE_AUTO_TECH_SUPPORT=$(ENABLE_AUTO_TECH_SUPPORT) \

build_image.sh

Lines changed: 1 addition & 1 deletion
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_DEV_SIGNING_CERT $SECURE_UPGRADE_DEV_SIGNING_KEY
9090
}
9191

9292
# Generate asic-specific device list

installer/sharch_body.sh

Lines changed: 6 additions & 2 deletions
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

Lines changed: 54 additions & 1 deletion
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

@@ -61,6 +64,13 @@ tmp_dir=
6164
clean_up()
6265
{
6366
rm -rf $tmp_dir
67+
if [ -n "$2" ]; then
68+
rm -rf "$2"
69+
if [ -n "$3" ];then
70+
rm -rf "$3"
71+
fi
72+
echo "Error: CMS signature not created - exiting without signing"
73+
fi
6474
exit $1
6575
}
6676

@@ -100,7 +110,7 @@ sed -i -e "s/%%DEMO_TYPE%%/$demo_type/g" \
100110
-e "s@%%OUTPUT_RAW_IMAGE%%@$output_raw_image@" \
101111
$tmp_installdir/install.sh || clean_up 1
102112
echo -n "."
103-
cp -r $* $tmp_installdir || clean_up 1
113+
cp -r $onie_installer_payload $tmp_installdir || clean_up 1
104114
echo -n "."
105115
[ -r "$platform_conf" ] && {
106116
cp $platform_conf $tmp_installdir || clean_up 1
@@ -130,7 +140,50 @@ cp $installer_dir/sharch_body.sh $output_file || {
130140
# Replace variables in the sharch template
131141
sed -i -e "s/%%IMAGE_SHA1%%/$sha1/" $output_file
132142
echo -n "."
143+
tar_size="$(wc -c < "${sharch}")"
133144
cat $sharch >> $output_file
145+
sed -i -e "s|%%PAYLOAD_IMAGE_SIZE%%|${tar_size}|" ${output_file}
146+
echo "secure upgrade flags: SECURE_UPGRADE_MODE = $SECURE_UPGRADE_MODE, \
147+
SECURE_UPGRADE_DEV_SIGNING_KEY = $SECURE_UPGRADE_DEV_SIGNING_KEY, SECURE_UPGRADE_DEV_SIGNING_CERT = $SECURE_UPGRADE_DEV_SIGNING_CERT"
148+
149+
if [ "$SECURE_UPGRADE_MODE" = "dev" -o "$SECURE_UPGRADE_MODE" = "prod" ]; then
150+
CMS_SIG="${tmp_dir}/signature.sig"
151+
DIR="$(dirname "$0")"
152+
scripts_dir="${DIR}/scripts"
153+
echo "$0 $SECURE_UPGRADE_MODE signing - creating CMS signature for ${output_file}. Output file ${CMS_SIG}"
154+
155+
if [ "$SECURE_UPGRADE_MODE" = "dev" ]; then
156+
echo "$0 dev keyfile location: ${key_file}."
157+
. ${scripts_dir}/sign_image_dev.sh || {
158+
echo "dev sign script ${scripts_dir}/sign_image_dev.sh not found"
159+
clean_up 1 ${output_file}
160+
}
161+
sign_image_dev ${cert_file} ${key_file} ${output_file} ${CMS_SIG} || {
162+
echo "CMS sign error $?"
163+
clean_up 1 ${CMS_SIG} ${output_file}
164+
}
165+
else # "$SECURE_UPGRADE_MODE" has to be equal to "prod"
166+
. ${scripts_dir}/sign_image_${machine}.sh || {
167+
echo "prod sign script ${scripts_dir}/sign_image_${machine}.sh not found"
168+
clean_up 1 ${output_file}
169+
}
170+
sign_image_prod ${output_file} ${CMS_SIG} ${SECURE_UPGRADE_MODE} || {
171+
echo "CMS sign error $?"
172+
clean_up 1 ${CMS_SIG} ${output_file}
173+
}
174+
fi
175+
176+
[ -f "$CMS_SIG" ] || {
177+
echo "Error: CMS signature not created - exiting without signing"
178+
clean_up 1
179+
}
180+
# append signature to binary
181+
cat ${CMS_SIG} >> ${output_file}
182+
sudo rm -rf ${CMS_SIG}
183+
elif [ "$SECURE_UPGRADE_MODE" -ne "no_sign" ]; then
184+
echo "SECURE_UPGRADE_MODE not defined or defined as $SECURE_UPGRADE_MODE - build without signing"
185+
fi
186+
134187
rm -rf $tmp_dir
135188
echo " Done."
136189

scripts/sign_image_dev.sh

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

0 commit comments

Comments
 (0)