summaryrefslogtreecommitdiffstats
path: root/Scripts/decrypt_smime_file.sh
blob: f0b5d4fde36476f25a5f4451e6e68a1d89f6dea0 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/sh
# Copyright 2020, Ludwig V. <https://github.com/ludwig-v>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License at <http://www.gnu.org/licenses/> for
# more details.

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

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