summaryrefslogtreecommitdiffstats
path: root/Scripts/decrypt_smime_file.sh
blob: 3045799ceb39d76e46c1fda16f284b9db55c5964 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/sh

FILE=$1
FIRMWARE_KEY=$2

if [[ -f "$FILE" ]]; then
	DATA=$(head -2 $FILE | grep "smime." 2>&1)
	if [[ $DATA =~ '"smime.' ]]; then
		cp $FILE $FILE.bak
		OPENSSL_RESULT=$(openssl smime -decrypt -in $FILE.bak -inkey $FIRMWARE_KEY  2>&1 > $FILE)
		if [[ -f "$FILE" && $OPENSSL_RESULT == '' ]]; then
			echo "${FILE} has been decrypted"
			rm -rf $FILE.bak
		else
			mv $FILE.bak $FILE
			echo "${FILE} has not been decrypted, the key is probably invalid !"
			echo "Stopping"
			exit 1
		fi
	else
		echo "${FILE} seems already decrypted !"
	fi
else
	echo "${FILE} not found !"
fi

exit 0