From d657708df25fb8d29f7091f35bb5809668ff3b91 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 6 Jan 2024 20:00:14 +0100 Subject: add contrib/unber.py utility This tool is a replacement for asn1c 'unber' program with a much more useful/readable output: * contains hexadecimal raw tag values * contains hexdump of value, rather than HTML entities in pseudo-XML Change-Id: I22c1a461ccba04c2c8caaab7ca29ea6ae76e2ea3 --- contrib/unber.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 contrib/unber.py diff --git a/contrib/unber.py b/contrib/unber.py new file mode 100755 index 0000000..65262e1 --- /dev/null +++ b/contrib/unber.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# A more useful verion of the 'unber' tool provided with asn1c: +# Give a hierarchical decode of BER/DER-encoded ASN.1 TLVs + +import sys +import argparse + +from pySim.utils import bertlv_parse_one, bertlv_encode_tag, b2h, h2b + +def process_one_level(content: bytes, indent: int): + remainder = content + while len(remainder): + tdict, l, v, remainder = bertlv_parse_one(remainder) + #print(tdict) + rawtag = bertlv_encode_tag(tdict) + if tdict['constructed']: + print("%s%s l=%d" % (indent*" ", b2h(rawtag), l)) + process_one_level(v, indent + 1) + else: + print("%s%s l=%d %s" % (indent*" ", b2h(rawtag), l, b2h(v))) + + +option_parser = argparse.ArgumentParser(description='BER/DER data dumper') +group = option_parser.add_mutually_exclusive_group(required=True) +group.add_argument('--file', help='Input file') +group.add_argument('--hex', help='Input hexstring') + + +if __name__ == '__main__': + opts = option_parser.parse_args() + + if opts.file: + with open(opts.file, 'rb') as f: + content = f.read() + elif opts.hex: + content = h2b(opts.hex) + + process_one_level(content, 0) -- cgit v1.2.3