summaryrefslogtreecommitdiffstats
path: root/etc/smartauth/smartauth.sh.in
blob: 92a9c560759f97e61e95f5c33b94823b85382701 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash

# Smart Card Authentication Helper (c) 2009 Timothy Pearson
#
#    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 for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

get_file () {
	if [[ $COMMAND_MODE == "acos" ]]; then
		# Select EF $1 under DF 1000
		echo "$SELECT_FILE $1" > query
		scriptor_standalone query 1> response2
		echo $(cat response2)

		# Read binary
		echo "$READ_BINARY" > query
		scriptor_standalone query 1> response2
		authokresponse="90 00 : Normal processing"
		response1=$(cat response2 | grep "$authokresponse")
		if [[ $response1 != "" ]]; then
			cat response2 | tr -d '\n' > response4
			stringtoreplace="Using T=0 protocol00 B0 00 00 FF> 00 B0 00 00 FF< "
			newstring=""
			sed -i "s#${stringtoreplace}#${newstring}#g" response4
			stringtoreplace=" 90 00 : Normal processing."
			newstring=""
			sed -i "s#${stringtoreplace}#${newstring}#g" response4
			if [[ $2 == "text" ]]; then
				stringtoreplace=" 00"
				newstring=""
				sed -i "s#${stringtoreplace}#${newstring}#g" response4
			fi
			echo $(cat response4)
			rm -f lukskey
			xxd -r -p response4 lukskey
			RESPONSE=lukskey
		fi
	fi

	if [[ $COMMAND_MODE == "cryptoflex" ]]; then
		echo "get $1" | opensc-explorer
		RESPONSE="3F00_$1"
	fi
}

# Initialize pcscd
killall pcscd &
sleep 1
pcscd &
sleep 1

# Get card ATR
echo "RESET" > query
scriptor_standalone query 1> response2
authokresponse="OK: "
response1=$(cat response2 | grep "$authokresponse")
if [[ $response1 != "" ]]; then
	cat response2 | tr -d '\n' > response4
	stringtoreplace="Using T=0 protocolRESET> RESET< OK: "
	newstring=""
	sed -i "s#${stringtoreplace}#${newstring}#g" response4
	smartatr=$(cat response4)
	echo "Got ATR: $smartatr"
	if [[ $smartatr == "3B BE 18 00 00 41 05 10 00 00 00 00 00 00 00 00 00 90 00 " ]]; then
		echo "Detected ACOS5 card"
		COMMAND_MODE="acos"
	fi
	if [[ $smartatr == "3B 02 14 50 " ]]; then
		echo "Detected Schlumberger CryptoFlex card"
		COMMAND_MODE="cryptoflex"
	fi
else
	echo "No card detected!"
	exit 1
fi

if [[ $COMMAND_MODE == "cryptoflex" ]]; then
	GET_CHALLENGE="C0 84 00 00 08"
	EXTERNAL_AUTH="C0 82 00 00 07 01"
	SELECT_FILE="C0 A4 00 00 02"
	DELETE_FILE="F0 E4 00 00 02"
fi

if [[ $COMMAND_MODE == "acos" ]]; then
	GET_CHALLENGE="00 84 00 00 08"
	EXTERNAL_AUTH="00 82 00 83 08"		# Key 3
	SELECT_FILE="00 A4 00 00 02"
	DELETE_FILE="00 E4 00 00 00"
	READ_BINARY="00 B0 00 00 FF"
	UPDATE_BINARY="00 D6 00 00 FF"
	ACTIVATE_FILE="00 44 00 00 02"
fi

# Authenticate card
if [[ $COMMAND_MODE == "acos" ]]; then
	# Select MF
	echo "00 A4 00 00 00" > query
	scriptor_standalone query 1> response2
	echo $(cat response2)

	# Select DF 1000 under MF
	echo "$SELECT_FILE 10 00" > query
	scriptor_standalone query 1> response2
	echo $(cat response2)
fi

echo $GET_CHALLENGE > authscript

scriptor_standalone authscript | grep 'Normal processing' > challenge
perl -pi -e 's/ //g' challenge
perl -pi -e 's/:Normalprocessing.//g' challenge
perl -pi -e 's/<//g' challenge
xxd -r -p challenge challenge

# Now DES encrypt the challenge
# Later, change the initialization vector to random if possible
openssl des-ecb -in challenge -out response -K <your key in hexidecimal> -iv 1

if [[ $COMMAND_MODE == "acos" ]]; then
	# Truncate to 8 bytes
	dd if=response of=response2 bs=1 count=8

	# Expand to standard hex listing format
	xxd -g 1 response2 response
	dd if=response of=response2 bs=1 count=23 skip=9
fi

if [[ $COMMAND_MODE == "cryptoflex" ]]; then
	# Truncate to 6 bytes
	dd if=response of=response2 bs=1 count=6

	# Expand to standard hex listing format
	xxd -g 1 response2 response
	dd if=response of=response2 bs=1 count=17 skip=9
fi

# Assemble the response file
response2=$(cat response2)
response1="$EXTERNAL_AUTH ${response2}"
echo $response1 > response

# Send the response!
scriptor_standalone response > response2

# Get the result
authokresponse="< 90 00 : Normal processing"
response1=$(cat response2 | grep "$authokresponse")
echo $response1
if [[ $response1 != "" ]]; then
	echo "Smart card validation successfull!"
	# Get encryption key
	if [[ $COMMAND_MODE == "acos" ]]; then
		get_file "10 01"
	fi

	if [[ $COMMAND_MODE == "cryptoflex" ]]; then
		get_file "1001"
	fi
	mv $RESPONSE smart.key
else
	echo "Authentication failed!"
fi

rm authscript &
rm response &
rm response2 &
rm challenge &