Verified Commit d8859e3b authored by Vojtech Myslivec's avatar Vojtech Myslivec 🚀

Use exception to handle error cases

parent daa047ad
......@@ -6,6 +6,7 @@ import sys
import os
from .argparser import parse_args, hash_type
from .exceptions import MoxOtpApiError, MoxOtpSetupError, MoxOtpUsageError
SYSFS_ROOT = "/sys/devices/platform/soc/soc:internal-regs@d0000000/soc:internal-regs@d0000000:crypto@0/"
......@@ -25,8 +26,7 @@ def errprint(*args, **kwargs):
def check_sysfs():
if not os.path.isdir(SYSFS_ROOT):
errprint("sysfs root directory does not exists (probably not running on MOX device)")
exit(2)
raise MoxOtpSetupError("sysfs root directory does not exists (probably not running on MOX device)")
def first_line_of_file(filename):
......@@ -39,20 +39,17 @@ def check_serial():
try:
first_line_of_file(SERIAL_PATH)
except (FileNotFoundError, PermissionError):
errprint("The sysfs API is probably broken – could not find MOX serial file")
exit(3)
raise MoxOtpApiError("Could not find MOX serial file")
def check_pubkey():
try:
pubkey = first_line_of_file(PUBKEY_PATH)
except (FileNotFoundError, PermissionError):
errprint("The sysfs API is probably broken – could not find MOX pubkey file")
exit(3)
raise MoxOtpApiError("Could not find MOX pubkey file")
if pubkey in ["", "\n", "none\n"]:
errprint("This device does not have its OTP key generated or accessible")
exit(2)
raise MoxOtpSetupError("This device does not have its OTP key generated or accessible")
def count_hash_from_file(f):
......@@ -75,8 +72,7 @@ def sign_hash(h):
with open(SIGN_PATH, "rb") as s:
sig = s.read(MAX_SIGNATURE_LENGTH)
except (FileNotFoundError, PermissionError):
errprint("The sysfs API is probably broken – could not find MOX sign file")
exit(3)
raise MoxOtpApiError("Could not find MOX sign file")
return sig[2:68] + sig[70:]
......@@ -120,24 +116,36 @@ def do_sign_hash(hexstr):
def main():
args = parse_args()
try:
args = parse_args()
if args.command in ["serial-number", "serial"]:
do_serial()
if args.command in ["serial-number", "serial"]:
do_serial()
elif args.command in ["public-key", "pubkey", "key"]:
do_pubkey()
elif args.command in ["public-key", "pubkey", "key"]:
do_pubkey()
elif args.command == "sign":
do_sign(args.infile)
elif args.command == "sign":
do_sign(args.infile)
elif args.command == "sign-hash":
do_sign_hash(args.hash)
elif args.command == "sign-hash":
do_sign_hash(args.hash)
else:
errprint("Unknown command '{}'".format(args.command))
else:
raise MoxOtpUsageError("Unknown command '{}'".format(args.command))
except MoxOtpUsageError as e:
errprint("usage error: {}".format(e))
exit(1)
except MoxOtpSetupError as e:
errprint("error: {}".format(e))
exit(2)
except MoxOtpApiError as e:
errprint("sysfs API error: {}".format(e))
exit(3)
if __name__ == "__main__":
main()
......@@ -6,6 +6,8 @@ import argparse
import hashlib
import sys
from .exceptions import MoxOtpSetupError
# hash algorithm used for message signature
HASH_TYPE = "sha512"
......@@ -18,8 +20,7 @@ def hash_type():
try:
h = hashlib.new(HASH_TYPE)
except ValueError:
errprint("Hash type {} is not available".format(HASH_TYPE))
exit(3)
raise MoxOtpSetupError("Hash type {} is not available".format(HASH_TYPE))
return h
......
"""
MOX OTP exceptions
"""
class MoxOtpError(Exception):
pass
class MoxOtpUsageError(MoxOtpError):
pass
class MoxOtpSetupError(MoxOtpError):
pass
class MoxOtpApiError(MoxOtpError):
pass
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