Commit c090f82b authored by Vojtech Myslivec's avatar Vojtech Myslivec 🚀

Move file and hash checks to separate functions

Also move checks into wrapper functions
parent 3c29d01a
...@@ -80,6 +80,34 @@ debug() { ...@@ -80,6 +80,34 @@ debug() {
} }
# --------------------------------------------------------------------
# checks existence and readability of file $1
check_file() {
local file=$1
[ -f "$file" -a -r "$file" ] || {
error "'$file' is not a readable file"
return 1
}
}
# check hexstring $1 of given length $2
check_hexstring() {
local hash=$1
local length=$2
[ -z "$(echo "$hash" | tr -d '0-9a-f')" ] || {
error 'Given hash is not hexadecimal string'
return 1
}
[ "${#hash}" -eq "$length" ] || {
error "Given hash must have $length hexadecimal characters"
return 1
}
}
# -------------------------------------------------------------------- # --------------------------------------------------------------------
hash_file() { hash_file() {
openssl "$HASH_TYPE" "$file" | awk '{print $2}' openssl "$HASH_TYPE" "$file" | awk '{print $2}'
...@@ -255,6 +283,7 @@ cached_atsha_serial() { ...@@ -255,6 +283,7 @@ cached_atsha_serial() {
# 64-bytes hex string from stdin # 64-bytes hex string from stdin
cached_atsha_challenge_response() { cached_atsha_challenge_response() {
local hash="$1" local hash="$1"
check_hexstring "$hash" "$HASH_LENGTH_ATSHA"
echo "$hash" \ echo "$hash" \
| cached_command string "$hash" 'atsha204cmd' 'challenge-response' | cached_command string "$hash" 'atsha204cmd' 'challenge-response'
...@@ -263,6 +292,7 @@ cached_atsha_challenge_response() { ...@@ -263,6 +292,7 @@ cached_atsha_challenge_response() {
cached_atsha_challenge_response_file() { cached_atsha_challenge_response_file() {
local file="$1" local file="$1"
check_file "$file"
# this is wierd atsha204cmd interface # this is wierd atsha204cmd interface
echo "$file" \ echo "$file" \
...@@ -278,12 +308,16 @@ cached_otp_serial() { ...@@ -278,12 +308,16 @@ cached_otp_serial() {
# 128-bytes hex string from stdin # 128-bytes hex string from stdin
cached_otp_sign_hash() { cached_otp_sign_hash() {
local hash="$1" local hash="$1"
check_hexstring "$hash" "$HASH_LENGTH_OTP"
cached_command string "$hash" 'mox-otp' 'sign-hash' "$hash" cached_command string "$hash" 'mox-otp' 'sign-hash' "$hash"
} }
cached_otp_sign() { cached_otp_sign() {
local file="$1" local file="$1"
check_file "$file"
cached_command file "$file" 'mox-otp' 'sign' "$file" cached_command file "$file" 'mox-otp' 'sign' "$file"
} }
...@@ -361,11 +395,6 @@ do_sign() { ...@@ -361,11 +395,6 @@ do_sign() {
file="$tmp" file="$tmp"
} }
[ -f "$file" -a -r "$file" ] || {
error "'$file' is not a readable file"
return 1
}
device_type=$(get_device_type) device_type=$(get_device_type)
case "$device_type" in case "$device_type" in
"$TYPE_ATSHA") "$TYPE_ATSHA")
...@@ -394,30 +423,15 @@ do_sign_hash() { ...@@ -394,30 +423,15 @@ do_sign_hash() {
# avoid multiline variable and capital letters # avoid multiline variable and capital letters
# busybox does not support neither ${var,,} nor tr [:upper:] [:lower:] # busybox does not support neither ${var,,} nor tr [:upper:] [:lower:]
local hash device_type local hash device_type
cache_init
hash=$(echo "${1}" | head -n 1 | tr 'A-Z' 'a-z') hash=$(echo "${1}" | head -n 1 | tr 'A-Z' 'a-z')
[ -z "$(echo "$hash" | tr -d '0-9a-f')" ] || { cache_init
error 'Given hash is not hexadecimal string'
return 1
}
device_type=$(get_device_type) device_type=$(get_device_type)
if [ "$device_type" = "$TYPE_ATSHA" ]; then if [ "$device_type" = "$TYPE_ATSHA" ]; then
[ "${#hash}" -eq "$HASH_LENGTH_ATSHA" ] || {
error "Hash for atsha must have $HASH_LENGTH_ATSHA hexadecimal characters"
return 1
}
debug "Call atsha challenge-response with '$hash'" debug "Call atsha challenge-response with '$hash'"
cached_atsha_challenge_response "$hash" cached_atsha_challenge_response "$hash"
elif [ "$device_type" = "$TYPE_OTP" ]; then elif [ "$device_type" = "$TYPE_OTP" ]; then
[ "${#hash}" -eq "$HASH_LENGTH_OTP" ] || {
error "Hash for atsha must have $HASH_LENGTH_OTP hexadecimal characters"
return 1
}
debug "Call otp sign-hash with '$hash'" debug "Call otp sign-hash with '$hash'"
cached_otp_sign_hash "$hash" cached_otp_sign_hash "$hash"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment