decoder.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
  5. # License: http://pyasn1.sf.net/license.html
  6. #
  7. from pyasn1.type import univ
  8. from pyasn1.codec.ber import decoder
  9. from pyasn1.compat.octets import oct2int
  10. from pyasn1 import error
  11. __all__ = ['decode']
  12. class BooleanDecoder(decoder.AbstractSimpleDecoder):
  13. protoComponent = univ.Boolean(0)
  14. def valueDecoder(self, fullSubstrate, substrate, asn1Spec, tagSet, length,
  15. state, decodeFun, substrateFun):
  16. head, tail = substrate[:length], substrate[length:]
  17. if not head or length != 1:
  18. raise error.PyAsn1Error('Not single-octet Boolean payload')
  19. byte = oct2int(head[0])
  20. # CER/DER specifies encoding of TRUE as 0xFF and FALSE as 0x0, while
  21. # BER allows any non-zero value as TRUE; cf. sections 8.2.2. and 11.1
  22. # in http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  23. if byte == 0xff:
  24. value = 1
  25. elif byte == 0x00:
  26. value = 0
  27. else:
  28. raise error.PyAsn1Error('Unexpected Boolean payload: %s' % byte)
  29. return self._createComponent(asn1Spec, tagSet, value), tail
  30. # TODO: prohibit non-canonical encoding
  31. BitStringDecoder = decoder.BitStringDecoder
  32. OctetStringDecoder = decoder.OctetStringDecoder
  33. RealDecoder = decoder.RealDecoder
  34. tagMap = decoder.tagMap.copy()
  35. tagMap.update(
  36. {univ.Boolean.tagSet: BooleanDecoder(),
  37. univ.BitString.tagSet: BitStringDecoder(),
  38. univ.OctetString.tagSet: OctetStringDecoder(),
  39. univ.Real.tagSet: RealDecoder()}
  40. )
  41. typeMap = decoder.typeMap
  42. class Decoder(decoder.Decoder):
  43. pass
  44. #: Turns CER octet stream into an ASN.1 object.
  45. #:
  46. #: Takes CER octetstream and decode it into an ASN.1 object
  47. #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
  48. #: may be a scalar or an arbitrary nested structure.
  49. #:
  50. #: Parameters
  51. #: ----------
  52. #: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
  53. #: CER octetstream
  54. #:
  55. #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  56. #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
  57. #: being decoded, *asn1Spec* may or may not be required. Most common reason for
  58. #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
  59. #:
  60. #: Returns
  61. #: -------
  62. #: : :py:class:`tuple`
  63. #: A tuple of pyasn1 object recovered from CER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  64. #: and the unprocessed trailing portion of the *substrate* (may be empty)
  65. #:
  66. #: Raises
  67. #: ------
  68. #: : :py:class:`pyasn1.error.PyAsn1Error`
  69. #: On decoding errors
  70. decode = Decoder(tagMap, decoder.typeMap)