-
Notifications
You must be signed in to change notification settings - Fork 28
/
yubitouch.sh
executable file
·179 lines (154 loc) · 3.78 KB
/
yubitouch.sh
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
178
179
#!/bin/sh
# Shell script for reading, setting or clearing touch requirements for
# cryptographic operations using the OpenPGP application on a
# YubiKey 4 or YubiKey 5.
#
# Author: Alessio Di Mauro <[email protected]>
GCA=$(command -v gpg-connect-agent)
XXD=$(command -v xxd)
OD=$(command -v od)
DO=0
UIF=0
ascii_to_hex()
{
if [ -n "$XXD" ]
then
$XXD -ps
elif [ -n "$OD" ]
then
$OD -An -tx1
fi
}
PE=$(command -v pinentry)
PE_PROMPT='SETPROMPT Admin PIN\nGETPIN\nBYE\n'
if [ -z "$GCA" ]
then
echo "Can not find gpg-connect-agent. Aborting..." >&2
exit 1;
fi
if [ -z "$XXD" ] && [ -z "$OD" ]
then
echo "Can not find xxd(1) nor od(1). Aborting..." >&2
exit 1;
fi
if [ $# -lt 2 ] || [ $# -gt 3 ]
then
echo "Wrong parameters" >&2
echo "usage: yubitouch {all|sig|aut|dec|att} {get|off|on|fix|cacheon|cachefix} [admin_pin]" >&2
exit 1;
fi
if [ "$1" = "all" ]
then
Keys="sig dec aut att"
else
Keys=$1
fi
for k in $Keys; do
if [ "$k" = "sig" ]
then
DO="D6"
elif [ "$k" = "dec" ]
then
DO="D7"
elif [ "$k" = "aut" ]
then
DO="D8"
elif [ "$k" = "att" ]
then
DO="D9"
else
echo "Invalid key value '$k' (must be sig, aut, dec, att). Aborting..." >&2
exit 1
fi
if [ "$2" = "get" ]
then
"$GCA" --hex "scd reset" /bye > /dev/null
GET=$("$GCA" --hex "scd apdu 00 ca 00 $DO 00" /bye)
if ! echo "$GET" | grep -q "90 00"
then
echo "Get data failed for $k, unsupported device?" >&2
exit 1
fi
STATUS=$(echo "$GET" | grep -oE "[0-9]{2} 20 90 00" | cut -c 1-2)
if [ "$STATUS" = "00" ]
then
UIF="off"
elif [ "$STATUS" = "01" ]
then
UIF="on"
elif [ "$STATUS" = "02" ]
then
UIF="fix"
elif [ "$STATUS" = "03" ]
then
UIF="cacheon"
elif [ "$STATUS" = "04" ]
then
UIF="cachefix"
else
echo "Unknown touch setting status ($STATUS)" >&2
exit 1
fi
echo "Current $k touch setting: $UIF" >&2
continue
elif [ "$2" = "off" ]
then
UIF="00";
elif [ "$2" = "on" ]
then
UIF="01"
elif [ "$2" = "fix" ]
then
UIF="02";
elif [ "$2" = "cacheon" ]
then
UIF="03";
elif [ "$2" = "cachefix" ]
then
UIF="04";
else
echo "Invalid value $2 (must be get, off, on, fix, cacheon, cachefix). Aborting..." >&2
exit 1
fi
echo "Processing key $k with action $2"
if [ $# -eq 3 ] && [ -z "$PIN" ]
then
PIN="$3"
elif [ -z "$PE" ] && [ -z "$PIN" ]
then
echo "Pinentry not present" >&2
echo "Falling back to regular stdin." >&2
echo "Be careful!" >&2
echo "Enter your admin PIN: "
read -r PIN
elif [ -z "$PIN" ]
then
CURRENT_TTY=$(tty)
# shellcheck disable=SC2059
PIN=$(printf "$PE_PROMPT" | $PE --ttyname "$CURRENT_TTY" | sed -n '/^D .*/s/^D //p')
fi
if [ -z "$PIN" ]
then
echo "Empty PIN. Aborting..." >&2
exit 1
fi
PIN_LEN=${#PIN}
# shellcheck disable=SC2059
PIN_HEX=$(printf "$PIN" | ascii_to_hex | tr -d '\n')
PIN_LEN_HEX=$(printf %02x "$PIN_LEN")
"$GCA" --hex "scd reset" /bye > /dev/null
VERIFY=$("$GCA" --hex "scd apdu 00 20 00 83 $PIN_LEN_HEX $PIN_HEX" /bye)
if ! echo "$VERIFY" | grep -q "90 00"
then
echo "Verification failed, wrong pin?" >&2
exit 1
fi
PUT=$("$GCA" --hex "scd apdu 00 da 00 $DO 02 $UIF 20" /bye)
if ! echo "$PUT" | grep -q "90 00"
then
echo "Unable to change mode for key $k. Set to fix?" >&2
exit 1
fi
done
echo "All done!"
exit 0