X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=pyderasn.py;h=e08f6bad3a090204af25441d78c285fec6521034;hb=a53922e56add84b7faf8b4ea6194406c5eedbc48;hp=505f3baafac8516084242256bb12f2633f02ace0;hpb=bba84f2f3b5fbc860f51cf76f2f504cb2aa8d899;p=pyderasn.git diff --git a/pyderasn.py b/pyderasn.py index 505f3ba..e08f6ba 100755 --- a/pyderasn.py +++ b/pyderasn.py @@ -4,7 +4,7 @@ # pylint: disable=line-too-long,superfluous-parens,protected-access,too-many-lines # pylint: disable=too-many-return-statements,too-many-branches,too-many-statements # PyDERASN -- Python ASN.1 DER/CER/BER codec with abstract structures -# Copyright (C) 2017-2020 Sergey Matveev +# Copyright (C) 2017-2021 Sergey Matveev # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as @@ -17,9 +17,9 @@ # # You should have received a copy of the GNU Lesser General Public # License along with this program. If not, see . -"""Python ASN.1 DER/BER codec with abstract structures +"""Python ASN.1 DER/CER/BER codec with abstract structures -This library allows you to marshal various structures in ASN.1 DER +This library allows you to marshal various structures in ASN.1 DER/CER format, unmarshal BER/CER/DER ones. >>> i = Integer(123) @@ -355,6 +355,11 @@ Let's parse that output, human:: (and its derivatives), ``SET``, ``SET OF``, ``UTCTime``, ``GeneralizedTime`` could be BERed. +Also it could be helpful to add quick ASN.1 pprinting command in your +pdb's configuration file:: + + alias pp1 import pyderasn ;; print(pyderasn.pprint(%1, oid_maps=(locals().get("OID_STR_TO_NAME", {}),))) + .. _definedby: DEFINED BY @@ -765,7 +770,7 @@ forcefully encoded in DER during CER encoding, by specifying class SignedAttributes(SetOf): schema = Attribute() - bounds = (1, 32) + bounds = (1, float("+inf")) der_forced = True .. _agg_octet_string: @@ -1170,8 +1175,6 @@ from datetime import datetime from datetime import timedelta from io import BytesIO from math import ceil -from mmap import mmap -from mmap import PROT_READ from operator import attrgetter from string import ascii_letters from string import digits @@ -1201,7 +1204,7 @@ except ImportError: # pragma: no cover def colored(what, *args, **kwargs): return what -__version__ = "7.7" +__version__ = "8.4" __all__ = ( "agg_octet_string", @@ -1292,8 +1295,13 @@ def file_mmaped(fd): :param fd: file object :returns: memoryview over read-only mmap-ing of the whole file + + .. warning:: + + It is known to work under neither Python 2.x nor Windows. """ - return memoryview(mmap(fd.fileno(), 0, prot=PROT_READ)) + import mmap + return memoryview(mmap.mmap(fd.fileno(), length=0, prot=mmap.PROT_READ)) def pureint(value): @@ -5080,13 +5088,20 @@ class UTCTime(VisibleString): .. warning:: - Pay attention that UTCTime can not hold full year, so all years - having < 50 years are treated as 20xx, 19xx otherwise, according - to X.509 recommendation. + Only **naive** ``datetime`` objects are supported. + Library assumes that all work is done in UTC. .. warning:: - No strict validation of UTC offsets are made, but very crude: + Pay attention that ``UTCTime`` can not hold full year, so all years + having < 50 years are treated as 20xx, 19xx otherwise, according to + X.509 recommendation. Use ``GeneralizedTime`` instead for + removing ambiguity. + + .. warning:: + + No strict validation of UTC offsets are made (only applicable to + **BER**), but very crude: * minutes are not exceeding 60 * offset value is not exceeding 14 hours @@ -5213,6 +5228,8 @@ class UTCTime(VisibleString): if isinstance(value, self.__class__): return value._value, None if value.__class__ == datetime: + if value.tzinfo is not None: + raise ValueError("only naive datetime supported") return self._dt_sanitize(value), None raise InvalidValueType((self.__class__, datetime)) @@ -5323,23 +5340,23 @@ class GeneralizedTime(UTCTime): .. warning:: - Only microsecond fractions are supported in DER encoding. - :py:exc:`pyderasn.DecodeError` will be raised during decoding of - higher precision values. + Only **naive** datetime objects are supported. + Library assumes that all work is done in UTC. .. warning:: - BER encoded data can loss information (accuracy) during decoding - because of float transformations. + Only **microsecond** fractions are supported in DER encoding. + :py:exc:`pyderasn.DecodeError` will be raised during decoding of + higher precision values. .. warning:: - Local times (without explicit timezone specification) are treated - as UTC one, no transformations are made. + **BER** encoded data can loss information (accuracy) during + decoding because of float transformations. .. warning:: - Zero year is unsupported. + **Zero** year is unsupported. """ __slots__ = () tag_default = tag_encode(24) @@ -7658,7 +7675,7 @@ def browse(raw, obj, oid_maps=()): self._state_update() return super(TW, self).selectable() - def get_display_text(self): + def _get_display_text_without_offset(self): pp, constructed = self._get_pp() style = "constructed" if constructed else "" if len(pp.decode_path) == 0: @@ -7676,6 +7693,11 @@ def browse(raw, obj, oid_maps=()): )) return (style, ent) + def get_display_text(self): + pp, _ = self._get_pp() + style, ent = self._get_display_text_without_offset() + return [(style, ent), " [%d]" % pp.offset] + def _scroll(self, what, step): self.state[what]._invalidate() pos = self.state[what].focus_position @@ -7888,12 +7910,12 @@ def main(): # pragma: no cover help="Path to BER/CER/DER file you want to decode", ) args = parser.parse_args() - if PY2: + try: + raw = file_mmaped(args.RAWFile)[args.skip:] + except: args.RAWFile.seek(args.skip) raw = memoryview(args.RAWFile.read()) args.RAWFile.close() - else: - raw = file_mmaped(args.RAWFile)[args.skip:] oid_maps = ( [obj_by_path(_path) for _path in (args.oids or "").split(",")] if args.oids else ()