diff options
author | Philipp Maier <pmaier@sysmocom.de> | 2020-05-11 21:35:01 +0200 |
---|---|---|
committer | Harald Welte <laforge@osmocom.org> | 2020-05-17 09:37:40 +0200 |
commit | e8536c04bc48daa4273124de25218cba7e547cc2 (patch) | |
tree | 732069343509feb64d913f5e3d13bbf244289091 /pySim | |
parent | 7f9f64ac2a239659b99a52809669e300bd20f75b (diff) |
pysim-prog: move ADM sanitation to utils.py
The lower part of gen_parameters() in pySim-prog.py contains some code
that checks whether the ADM pin supplied in its hexadecimal or in its
string form and generates a sanitised pin_adm from that. Lets separate
this part as it may become useful elsewhere too.
Change-Id: Ifead29724cc13a91de9e2e89314d7fb23c063d50
Diffstat (limited to 'pySim')
-rw-r--r-- | pySim/utils.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/pySim/utils.py b/pySim/utils.py index 43616a9..b5203dc 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -439,6 +439,37 @@ def dec_epdgid(hexstr): return s +def sanitize_pin_adm(opts): + """ + The ADM pin can be supplied either in its hexadecimal form or as + ascii string. This function checks the supplied opts parameter and + returns the pin_adm as hex encoded string, regardles in which form + it was originally supplied by the user + """ + + pin_adm = None + + if opts.pin_adm is not None: + if len(opts.pin_adm) <= 8: + pin_adm = ''.join(['%02x'%(ord(x)) for x in opts.pin_adm]) + pin_adm = rpad(pin_adm, 16) + + else: + raise ValueError("PIN-ADM needs to be <=8 digits (ascii)") + + if opts.pin_adm_hex is not None: + if len(opts.pin_adm_hex) == 16: + pin_adm = opts.pin_adm_hex + # Ensure that it's hex-encoded + try: + try_encode = h2b(pin_adm) + except ValueError: + raise ValueError("PIN-ADM needs to be hex encoded using this option") + else: + raise ValueError("PIN-ADM needs to be exactly 16 digits (hex encoded)") + + return pin_adm + def init_reader(opts): """ Init card reader driver |