codekingpro/portable-devtools
115k
1#2# This file is part of pyasn1 software.3#4# Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>5# License: https://pyasn1.readthedocs.io/en/latest/license.html6#7import warnings8 9from pyasn1 import error10from pyasn1.codec.streaming import readFromStream11from pyasn1.codec.ber import decoder12from pyasn1.type import univ13 14__all__ = ['decode', 'StreamingDecoder']15 16SubstrateUnderrunError = error.SubstrateUnderrunError17 18 19class BooleanPayloadDecoder(decoder.AbstractSimplePayloadDecoder):20 protoComponent = univ.Boolean(0)21 22 def valueDecoder(self, substrate, asn1Spec,23 tagSet=None, length=None, state=None,24 decodeFun=None, substrateFun=None,25 **options):26 27 if length != 1:28 raise error.PyAsn1Error('Not single-octet Boolean payload')29 30 for chunk in readFromStream(substrate, length, options):31 if isinstance(chunk, SubstrateUnderrunError):32 yield chunk33 34 byte = chunk[0]35 36 # CER/DER specifies encoding of TRUE as 0xFF and FALSE as 0x0, while37 # BER allows any non-zero value as TRUE; cf. sections 8.2.2. and 11.1 38 # in https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf39 if byte == 0xff:40 value = 141 42 elif byte == 0x00:43 value = 044 45 else:46 raise error.PyAsn1Error('Unexpected Boolean payload: %s' % byte)47 48 yield self._createComponent(asn1Spec, tagSet, value, **options)49 50 51# TODO: prohibit non-canonical encoding52BitStringPayloadDecoder = decoder.BitStringPayloadDecoder53OctetStringPayloadDecoder = decoder.OctetStringPayloadDecoder54RealPayloadDecoder = decoder.RealPayloadDecoder55 56TAG_MAP = decoder.TAG_MAP.copy()57TAG_MAP.update(58 {univ.Boolean.tagSet: BooleanPayloadDecoder(),59 univ.BitString.tagSet: BitStringPayloadDecoder(),60 univ.OctetString.tagSet: OctetStringPayloadDecoder(),61 univ.Real.tagSet: RealPayloadDecoder()}62)63 64TYPE_MAP = decoder.TYPE_MAP.copy()65 66# Put in non-ambiguous types for faster codec lookup67for typeDecoder in TAG_MAP.values():68 if typeDecoder.protoComponent is not None:69 typeId = typeDecoder.protoComponent.__class__.typeId70 if typeId is not None and typeId not in TYPE_MAP:71 TYPE_MAP[typeId] = typeDecoder72 73 74class SingleItemDecoder(decoder.SingleItemDecoder):75 __doc__ = decoder.SingleItemDecoder.__doc__76 77 TAG_MAP = TAG_MAP78 TYPE_MAP = TYPE_MAP79 80 81class StreamingDecoder(decoder.StreamingDecoder):82 __doc__ = decoder.StreamingDecoder.__doc__83 84 SINGLE_ITEM_DECODER = SingleItemDecoder85 86 87class Decoder(decoder.Decoder):88 __doc__ = decoder.Decoder.__doc__89 90 STREAMING_DECODER = StreamingDecoder91 92 93#: Turns CER octet stream into an ASN.1 object.94#:95#: Takes CER octet-stream and decode it into an ASN.1 object96#: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which97#: may be a scalar or an arbitrary nested structure.98#:99#: Parameters100#: ----------101#: substrate: :py:class:`bytes`102#: CER octet-stream103#:104#: Keyword Args105#: ------------106#: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative107#: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure108#: being decoded, *asn1Spec* may or may not be required. Most common reason for109#: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.110#:111#: Returns112#: -------113#: : :py:class:`tuple`114#: A tuple of pyasn1 object recovered from CER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)115#: and the unprocessed trailing portion of the *substrate* (may be empty)116#:117#: Raises118#: ------119#: ~pyasn1.error.PyAsn1Error, ~pyasn1.error.SubstrateUnderrunError120#: On decoding errors121#:122#: Examples123#: --------124#: Decode CER serialisation without ASN.1 schema125#:126#: .. code-block:: pycon127#:128#: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00')129#: >>> str(s)130#: SequenceOf:131#: 1 2 3132#:133#: Decode CER serialisation with ASN.1 schema134#:135#: .. code-block:: pycon136#:137#: >>> seq = SequenceOf(componentType=Integer())138#: >>> s, _ = decode(b'0\x80\x02\x01\x01\x02\x01\x02\x02\x01\x03\x00\x00', asn1Spec=seq)139#: >>> str(s)140#: SequenceOf:141#: 1 2 3142#:143decode = Decoder()144 145def __getattr__(attr: str):146 if newAttr := {"tagMap": "TAG_MAP", "typeMap": "TYPE_MAP"}.get(attr):147 warnings.warn(f"{attr} is deprecated. Please use {newAttr} instead.", DeprecationWarning, stacklevel=2)148 return globals()[newAttr]149 raise AttributeError(attr)150 