-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathresetCard.sh
More file actions
executable file
·81 lines (68 loc) · 1.59 KB
/
Copy pathresetCard.sh
File metadata and controls
executable file
·81 lines (68 loc) · 1.59 KB
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
#!/usr/bin/env bash
# https://github.com/drduh/YubiKey-Guide/blob/main/scripts/resetCard.sh
#set -x # uncomment to debug
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail
# https://developers.yubico.com/ykneo-openpgp/ResetApplet.html
readonly CARD_CMD=$(cat <<EOF
/hex
scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
scd apdu 00 20 00 81 08 40 40 40 40 40 40 40 40
scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
scd apdu 00 20 00 83 08 40 40 40 40 40 40 40 40
scd apdu 00 e6 00 00
scd apdu 00 44 00 00
/bye
EOF
)
GPG_AGENT=""
getAgent() {
GPG_AGENT=$(command -v gpg-connect-agent) || {
printf "gpg-connect-agent not found" >&2
exit 1
}
[[ -x "$GPG_AGENT" ]] || {
printf "%s is not executable" "$GPG_AGENT" >&2
exit 1
}
readonly GPG_AGENT
"$GPG_AGENT" /bye > /dev/null 2>&1 || {
printf "gpg-connect-agent not available" >&2
exit 1
}
}
confirm() {
printf "Reset command will be sent to gpg-agent ..."
read -r -p " continue? [y/N] " reply
case "$reply" in
[Yy]) ;;
*) exit 0 ;;
esac
}
runCommands() {
local output
local error
if ! output=$(printf "%s\n" "$CARD_CMD" |
"$GPG_AGENT" 2>&1); then
printf "gpg-connect-agent failed" >&2
exit 1
fi
if error=$(grep "^ERR " <<<"$output" | head -n1); then
printf "%s" "$error" >&2
exit 1
fi
printf "%s" "$output"
}
main() {
getAgent
confirm
runCommands
printf "Done"
}
main "$@"