X-Git-Url: http://www.git.cypherpunks.ru/?p=pyderasn.git;a=blobdiff_plain;f=pyderasn.py;h=6db8017e12a60a7d7ddfa4202514c31f808bd39e;hp=92ffc17cd06e8ef16db431d0a399b376f2d7c2a8;hb=73db29cf680d46539f651e742acd1942414ce9a1;hpb=979545a22e931824c5ef016fd6ef7c471fc055d3 diff --git a/pyderasn.py b/pyderasn.py index 92ffc17..6db8017 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -213,6 +213,7 @@ decoding process. Currently available context options: +* :ref:`allow_expl_oob ` * :ref:`bered ` * :ref:`defines_by_path ` * :ref:`strict_default_existence ` @@ -393,6 +394,22 @@ constructed primitive types should be parsed successfully. EOC (end-of-contents) token's length is taken in advance in object's value length. +.. _allow_expl_oob_ctx: + +Allow explicit tag out-of-bound +------------------------------- + +Invalid BER encoding could contain ``EXPLICIT`` tag containing more than +one value, more than one object. If you set ``allow_expl_oob`` context +option to True, then no error will be raised and that invalid encoding +will be silently further processed. But pay attention that offsets and +lengths will be invalid in that case. + +.. warning:: + + This option should be used only for skipping some decode errors, just + to see the decoded structure somehow. + Primitive types --------------- @@ -1092,6 +1109,13 @@ class Obj(object): if tag_only: return obj, tail = result + if obj.tlvlen < l and not ctx.get("allow_expl_oob", False): + raise DecodeError( + "explicit tag out-of-bound, longer than data", + klass=self.__class__, + decode_path=decode_path, + offset=offset, + ) return obj, (tail if leavemm else tail.tobytes()) @property @@ -5246,6 +5270,11 @@ def main(): # pragma: no cover "--decode-path-only", help="Print only specified decode path", ) + parser.add_argument( + "--allow-expl-oob", + action="store_true", + help="Allow explicit tag out-of-bound", + ) parser.add_argument( "DERFile", type=argparse.FileType("rb"), @@ -5262,7 +5291,10 @@ def main(): # pragma: no cover pprinter = partial(pprint, big_blobs=True) else: schema, pprinter = generic_decoder() - ctx = {"bered": not args.nobered} + ctx = { + "bered": not args.nobered, + "allow_expl_oob": args.allow_expl_oob, + } if args.defines_by_path is not None: ctx["defines_by_path"] = obj_by_path(args.defines_by_path) obj, tail = schema().decode(der, ctx=ctx)