Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decoder.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #
  2. # This file is part of pyasn1 software.
  3. #
  4. # Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. from pyasn1.codec.cer import decoder
  8. from pyasn1.type import univ
  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.copy()
  23. # Put in non-ambiguous types for faster codec lookup
  24. for typeDecoder in tagMap.values():
  25. if typeDecoder.protoComponent is not None:
  26. typeId = typeDecoder.protoComponent.__class__.typeId
  27. if typeId is not None and typeId not in typeMap:
  28. typeMap[typeId] = typeDecoder
  29. class Decoder(decoder.Decoder):
  30. supportIndefLength = False
  31. #: Turns DER octet stream into an ASN.1 object.
  32. #:
  33. #: Takes DER octet-stream and decode it into an ASN.1 object
  34. #: (e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative) which
  35. #: may be a scalar or an arbitrary nested structure.
  36. #:
  37. #: Parameters
  38. #: ----------
  39. #: substrate: :py:class:`bytes` (Python 3) or :py:class:`str` (Python 2)
  40. #: DER octet-stream
  41. #:
  42. #: Keyword Args
  43. #: ------------
  44. #: asn1Spec: any pyasn1 type object e.g. :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
  45. #: A pyasn1 type object to act as a template guiding the decoder. Depending on the ASN.1 structure
  46. #: being decoded, *asn1Spec* may or may not be required. Most common reason for
  47. #: it to require is that ASN.1 structure is encoded in *IMPLICIT* tagging mode.
  48. #:
  49. #: Returns
  50. #: -------
  51. #: : :py:class:`tuple`
  52. #: A tuple of pyasn1 object recovered from DER substrate (:py:class:`~pyasn1.type.base.PyAsn1Item` derivative)
  53. #: and the unprocessed trailing portion of the *substrate* (may be empty)
  54. #:
  55. #: Raises
  56. #: ------
  57. #: :py:class:`~pyasn1.error.PyAsn1Error`
  58. #: On decoding errors
  59. #:
  60. #: Examples
  61. #: --------
  62. #: Decode DER serialisation without ASN.1 schema
  63. #:
  64. #: .. code-block:: pycon
  65. #:
  66. #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03')
  67. #: >>> str(s)
  68. #: SequenceOf:
  69. #: 1 2 3
  70. #:
  71. #: Decode DER serialisation with ASN.1 schema
  72. #:
  73. #: .. code-block:: pycon
  74. #:
  75. #: >>> seq = SequenceOf(componentType=Integer())
  76. #: >>> s, _ = decode(b'0\t\x02\x01\x01\x02\x01\x02\x02\x01\x03', asn1Spec=seq)
  77. #: >>> str(s)
  78. #: SequenceOf:
  79. #: 1 2 3
  80. #:
  81. decode = Decoder(tagMap, typeMap)