5 if [ ! -d ${TMP_DIR} ]; then
11 if [ -d ${TMP_DIR} ]; then
14 echo "********** Package Signing End **********"
18 # CheckFile FILE MESSAGE
26 # CheckNull VAR MESSAGE
45 echo "Checking argument..."
47 for ARGUMENT in ${ArgumentList[@]}; do
48 CheckFile ${ARGUMENT} ${ARGUMENT}" not exist"
51 FILE_SIZE=$(${STAT} -c %s ${FILE})
52 CheckNull ${FILE_SIZE} "Failed to get the size of file"
54 if [ -z ${SIGNED_FILE} ]; then
59 BASENAME=/usr/bin/basename
60 OPENSSL=/usr/bin/openssl
71 echo "Checking tool..."
73 for TOOL in ${ToolList[@]}; do
74 CheckFile ${TOOL} ${TOOL}" not exist"
80 ExtractFromPKCSFile() {
81 echo "Extract from PKCS file..."
83 KEY=${TMP_DIR}/key.pem
84 ${OPENSSL} pkcs12 -in ${PKCS} -nocerts -passin pass:${PKCS_PASSWORD} -passout pass:${PKCS_PASSWORD} -out ${KEY}
87 CERT=${TMP_DIR}/cert.pem
88 ${OPENSSL} pkcs12 -in ${PKCS} -clcerts -nokeys -passin pass:${PKCS_PASSWORD} -out ${CERT}
95 echo "Signing file..."
97 SIGNATURE=${TMP_DIR}/$(${BASENAME} ${FILE}).sign
98 CheckNull ${SIGNATURE} "Failed to name signature"
100 ${OPENSSL} dgst -sha256 -sign ${KEY} -passin pass:${PKCS_PASSWORD} -out ${SIGNATURE} ${FILE}
101 CheckFile ${SIGNATURE} "Failed to sign"
103 SIGNATURE_SIZE=$(${STAT} -c %s ${SIGNATURE})
104 CheckNull ${SIGNATURE_SIZE} "Failed to get the size of signature"
108 CERT_CONVERTED_SIZE=""
110 echo "Converting certificate..."
112 CERT_CONVERTED=${TMP_DIR}/$(${BASENAME} ${CERT}).der
113 CheckNull ${CERT_CONVERTED} "Failed to name converted certificate"
115 ${OPENSSL} x509 -in ${CERT} -outform DER -out ${CERT_CONVERTED}
116 CheckFile ${CERT_CONVERTED} "Failed to convert certificate"
118 CERT_CONVERTED_SIZE=$(${STAT} -c %s ${CERT_CONVERTED})
119 CheckNull ${CERT_CONVERTED_SIZE} "Failed to get the size of converted certificate"
123 MAGIC_NUMBER="TOTA_SIGNED_V1"
125 echo "Attaching signature..."
127 RESULT_FILE=${TMP_DIR}/result
129 cat ${SIGNATURE} ${CERT_CONVERTED} > ${RESULT_FILE}
130 ${PERL} -e "print pack('L', ${FILE_SIZE})" >> ${RESULT_FILE}
131 ${PERL} -e "print pack('L', ${SIGNATURE_SIZE})" >> ${RESULT_FILE}
132 ${PERL} -e "print pack('L', ${CERT_CONVERTED_SIZE})" >> ${RESULT_FILE}
133 echo -n ${MAGIC_NUMBER} >> ${RESULT_FILE}
137 echo "Verifying signature..."
139 EXPECTED_SIZE=$(expr ${#MAGIC_NUMBER} + ${SIGNATURE_SIZE} + ${CERT_CONVERTED_SIZE} + 12)
140 REAL_SIZE=$(${STAT} -c %s ${RESULT_FILE})
142 if [ ${EXPECTED_SIZE} -ne ${REAL_SIZE} ]; then
143 echo "Invalid result size : Expected("${EXPECTED_SIZE}") Real("${REAL_SIZE}")"
149 echo "Inserting signature..."
151 if [ ${FILE} != ${SIGNED_FILE} ]; then
152 cp ${FILE} ${SIGNED_FILE}
155 cat ${RESULT_FILE} >> ${SIGNED_FILE}
160 echo "********** Package Signing Start **********"
162 if [ "$#" -lt 3 ]; then
163 echo "Usage : sign_upg.sh PKCS_FILE PKCS_PASSWORD FILE_NAME [SIGNED_FILE_NAME]"
164 echo " - PKCS_FILE should include private key and certificate"
165 echo " - If SIGNED_FILE_NAME is NULL, signature will be overwritten to FILE_NAME"
180 echo "Succeed to sign file!"