#/bin/bash
# 
# The MIT License (MIT)
# Copyright (c) 2016 Intel Corporation
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 
# DE10-Nano SDCard image
#-----------------------
# This script aids with the authentication of the DE10-Nano SDCard image.
#
# To verify the signature of the sdcard the signature file is used to
# authenticate the origin of the image.  This script automates the following:
#
# 1) Save the signing chain de10-nano-image-Angstrom-v2016.12.socfpga-sdimg.sig
#    as a pem file
#
#   openssl pkcs7 -print_certs -inform der \
#     -in de10-nano-image-Angstrom-v2016.12.socfpga-sdimg.<VERSION>.tgz.sig > \
#     de10-nano-image-Angstrom-v2016.12.socfpga-sdimg.pem
#
# 2) Verify the signing certificate serial number
#
#   openssl x509 \
#     -in de10-nano-image-Angstrom-v2016.12.socfpga-sdimg.pem -serial -noout
#
# The result should be:
#
# serial=56000003003F7A8C8F5DD497E1000000000300
#
# 3) Verify the signature of the file
#
#   openssl smime -verify \
#     -in de10-nano-image-Angstrom-v2016.12.socfpga-sdimg.<VERSION>.tgz.sig \
#     -inform der \
#     -content de10-nano-image-Angstrom-v2016.12.socfpga-sdimg.<VERSION>.tgz \
#     -noverify de10-nano-image-Angstrom-v2016.12.socfpga-sdimg.pem > /dev/null
#
# The result should be
#
# Verification successful

if [[ $# -ne 1 ]]; then
	echo "Usage:"
	echo "    ./authentication.sig.sh <VERSION>"
	echo "      -> where version is the build date of the sdcard image tgz"
	echo "         file to be authenticated and the sdcard image is in the"
	echo "         same directory as the authentication.sig.sh script"
fi

VERSION=$1

BASE_FILENAME="de10-nano-image-Angstrom-v2016.12.socfpga-sdimg"

if [ -f "$BASE_FILENAME.$VERSION.tgz.sig" ]; then
	echo "Found signature file"
	openssl pkcs7 -print_certs -inform der \
	-in $BASE_FILENAME.$VERSION.tgz.sig > \
	$BASE_FILENAME.$VERSION.tgz.pem
else
	echo -n "Error: File not found -> "
	echo "$BASE_FILENAME.$VERSION.tgz.sig"
	exit 1
fi

if [ -f "$BASE_FILENAME.$VERSION.tgz.pem" ]; then
	echo "Check PEM serial number"
	echo "  Serial # should be 56000003003F7A8C8F5DD497E1000000000300"
	SER=$(openssl x509 -in $BASE_FILENAME.$VERSION.tgz.pem -serial -noout)
	echo "  $SER"
	if [ "$SER" == "serial=56000003003F7A8C8F5DD497E1000000000300" \]; then
		echo "Serial # matched"
	else
		echo "Error: Serial # does not match"
		exit 1
	fi
else 
	echo -n "Error: File not found -> "
	echo "$BASE_FILENAME.$VERSION.tgz.pem"
	exit 1
fi


if [ -f "$BASE_FILENAME.$VERSION.tgz" ]; then
	echo "Verifying signature of $BASE_FILENAME.$VERSION.tgz"
	openssl smime -verify -in $BASE_FILENAME.$VERSION.tgz.sig \
		-inform der \
		-content $BASE_FILENAME.$VERSION.tgz \
		-noverify $BASE_FILENAME.$VERSION.tgz.pem > /dev/null
else
	echo -n "Error: File not found -> "
	echo "$BASE_FILENAME.$VERSION.tgz"
	exit 1
fi
