decoder.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.cer import decoder
  9. __all__ = ['decode']
  10. class BitStringDecoder(decoder.BitStringDecoder):
  11. supportConstructedForm = False
  12. class OctetStringDecoder(decoder.OctetStringDecoder):
  13. supportConstructedForm = False
  14. # TODO: prohibit non-canonical encoding
  15. RealDecoder = decoder.RealDecoder
  16. tagMap = decoder.tagMap.copy()
  17. tagMap.update(
  18. {univ.BitString.tagSet: BitStringDecoder(),
  19. univ.OctetString.tagSet: OctetStringDecoder(),
  20. univ.Real.tagSet: RealDecoder()}
  21. )
  22. typeMap = decoder.typeMap
  23. class Decoder(decoder.Decoder):
  24. supportIndefLength = False
  25. #: Turns DER octet stream into an ASN.1 object.
  26. #:
  27. #: Takes DER octetstream and decode it into an ASN.1 object
  28. #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
  29. #: may be a scalar or an arbitrary nested structure.
  30. #:
  31. #: Parameters
  32. #: ----------
  33. #: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
  34. #: DER octetstream
  35. #:
  36. #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  37. #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
  38. #: being decoded, *asn1Spec* may or may not be required. Most common reason for
  39. #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
  40. #:
  41. #: Returns
  42. #: -------
  43. #: : :py:class:`tuple`
  44. #: A tuple of pyasn1 object recovered from DER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  45. #: and the unprocessed trailing portion of the *substrate* (may be empty)
  46. #:
  47. #: Raises
  48. #: ------
  49. #: : :py:class:`pyasn1.error.PyAsn1Error`
  50. #: On decoding errors
  51. decode = Decoder(tagMap, typeMap)