Skip to content

Commit 5962212

Browse files
authored
Merge pull request #33 from aeolwyr/master
Add termux-keystore
2 parents 6cfe809 + 903a956 commit 5962212

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

scripts/termux-keystore

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/data/data/com.termux/files/usr/bin/sh
2+
set -e -u
3+
4+
readonly CMD_BASE="/data/data/com.termux/files/usr/libexec/termux-api Keystore"
5+
6+
SCRIPTNAME=termux-keystore
7+
show_usage () {
8+
echo "Usage: $SCRIPTNAME command"
9+
echo "These commands are supported:"
10+
echo " list [-d]"
11+
echo " delete <alias>"
12+
echo " generate <alias> [-a alg] [-s size] [-u validity]"
13+
echo " sign <alias> <algorithm>"
14+
echo " verify <alias> <algorithm> <signature>"
15+
echo
16+
echo "list: List the keys stored inside the keystore."
17+
echo " -d Detailed results (includes key parameters)."
18+
echo
19+
echo "delete: Permanently delete a given key from the keystore."
20+
echo " alias Alias of the key to delete."
21+
echo
22+
echo "generate: Create a new key inside the hardware keystore."
23+
echo " alias Alias of the key."
24+
echo " -a alg Algorithm to use (either 'RSA' or 'EC'). Defaults to RSA."
25+
echo " -s size Key size to use. For RSA, the options are 2048, 3072"
26+
echo " and 4096. For EC, the options are 256, 384 and 521."
27+
echo " -u validity User validity duration in seconds. Omit to disable."
28+
echo " When enabled, the key can only be used for the"
29+
echo " duration specified after the device unlocks. After"
30+
echo " the duration has passed, the user needs to re-lock"
31+
echo " and unlock the device again to be able to use this key."
32+
echo
33+
echo "sign: Sign using the given key, the data is read from stdin and the"
34+
echo "signature is output to stdout."
35+
echo " alias Alias of the key to use for signing."
36+
echo " algorithm Algorithm to use, e.g. 'SHA256withRSA'. This should"
37+
echo " match the algorithm of the key."
38+
echo
39+
echo "verify: Verify a signature. The data (original file) is read from stdin."
40+
echo " alias Alias of the key to use for verify."
41+
echo " algorithm Algorithm that was used to sign this data."
42+
echo " signature Signature file to use in verification."
43+
}
44+
45+
46+
check_args () {
47+
if [ "$2" != "$3" ]; then
48+
echo "$SCRIPTNAME: $1 needs exactly $2 arguments"
49+
exit 1
50+
fi
51+
}
52+
53+
list_keys () {
54+
if [ "$#" -gt 0 ] && [ "$1" = "-d" ]; then
55+
$CMD_BASE -e command list --ez detailed true
56+
else
57+
$CMD_BASE -e command list
58+
fi
59+
}
60+
61+
delete_key () {
62+
check_args delete 1 $#
63+
$CMD_BASE -e command delete -e alias "$1"
64+
}
65+
66+
sign_data () {
67+
check_args sign 2 $#
68+
$CMD_BASE -e command sign -e alias "$1" -e algorithm "$2" | base64 -d
69+
}
70+
71+
verify_data () {
72+
check_args verify 3 $#
73+
$CMD_BASE -e command verify -e alias "$1" -e algorithm "$2" \
74+
-e signature "$(realpath "$3")"
75+
}
76+
77+
generate_key () {
78+
if [ $# -lt 1 ]; then
79+
echo "$SCRIPTNAME generate: alias argument is required"
80+
exit 1
81+
fi
82+
ALIAS=$1; shift
83+
ALGORITHM=RSA; SIZE=-1; CURVE=secp256r1; VALIDITY=0
84+
while getopts a:s:c:u: NAME; do
85+
case "$NAME" in
86+
a) ALGORITHM=$OPTARG ;;
87+
s) SIZE=$OPTARG ;;
88+
u) VALIDITY=$OPTARG ;;
89+
?) ;;
90+
esac
91+
done
92+
93+
if [ "$ALGORITHM" = "RSA" ]; then
94+
case "$SIZE" in
95+
-1) SIZE=2048 ;;
96+
2048|3072|4096) ;;
97+
*) echo "$SCRIPTNAME: invalid RSA key size $SIZE"; exit 1 ;;
98+
esac
99+
elif [ "$ALGORITHM" = "EC" ]; then
100+
case "$SIZE" in
101+
-1|256) CURVE=secp256r1 ;;
102+
384) CURVE=secp384r1 ;;
103+
521) CURVE=secp521r1 ;;
104+
*) echo "$SCRIPTNAME: invalid EC key size $SIZE"; exit 1 ;;
105+
esac
106+
else
107+
echo "$SCRIPTNAME: invalid algorithm $ALGORITHM"; exit 1
108+
fi
109+
110+
# purpose 12 is SIGN+VERIFY
111+
$CMD_BASE -e command generate -e alias "$ALIAS" -e algorithm "$ALGORITHM" \
112+
--ei purposes 12 --esa digests SHA-1,SHA-256,SHA-384,SHA-512 \
113+
--ei size "$SIZE" -e curve "$CURVE" --ei validity "$VALIDITY"
114+
}
115+
116+
ACTION="${1-}"
117+
if [ "$#" -gt 0 ]; then shift; fi
118+
119+
case "$ACTION" in
120+
list) list_keys "$@" ;;
121+
generate) generate_key "$@" ;;
122+
delete) delete_key "$@" ;;
123+
sign) sign_data "$@" ;;
124+
verify) verify_data "$@" ;;
125+
*) show_usage ;;
126+
esac

0 commit comments

Comments
 (0)